Constructing methods and objects in Java

Posted by elhelaly1999 on Sat, 20 Nov 2021 22:27:07 +0100

1, Concept and application of construction method

Constructor is a special method in. Its name must be exactly the same as that of its class, and there is no return value, and there is no need to use the keyword void for identification.
For example:

public class Apple{
	public Apple(){//Default construction method

1. Default construction method and user-defined construction method.

If a class instance defines one or more construction methods, the default construction method is not provided in java.
For example, an Apple class is defined to initialize member variables in the constructor of this class.

public class Apple {
	int num;//Declare member variables
	float price;
	Apple apple;
	public Apple() {//Declaration constructor
		num = 10;//Initializing member variables
		price= 8.34f;

	public static void main(String[] args) {
		Apple apple = new Apple();//Create an instance object for Apple
		System .out .print("Number of apples:"+apple.num);
		System .out .print("Unit price of apple:"+apple.price);
		System.out.print("Member variable apple=";


In Java, you can customize the parameterless constructor and the parameterless constructor, for example:

public class Apple {
	int num = 10;//Declare member variables
	public Apple() {//Declaration constructor
		num = 19;//Initializing member variables
	public Apple(int i) {
		num = i;

	public static void main(String[] args) {
		Apple apple = new Apple();//Create an instance object for Apple
		System .out .print("Number of apples:"+apple.num);
		Apple app = new Apple(8);
		System.out.println("Number of apples:"+app.num);

Output results:

Number of apples: 19 number of apples: 8

2. Description:

The difference between a constructor with or without parameters is that a constructor with parameters can assign values to the data in the created object at the same time when using new.

3. Note:

There is no type in the constructor!!!!!

2, Overview of objects

The declaration cycle of an object in the Java language includes three stages: creation, use and destruction.

1. Object creation:

(1) Object declaration:


Class name object name;

Class name: required; used to specify a defined class.
Object name: required to specify the object name. The object name must be a legal Java identifier.

Apple redApple;//Declare an object of the Apple class readApple

(2) Instantiate object

Explanation: when declaring an object, you just create a reference for it in memory and collocate the initial value null, indicating that it does not point to any memory space. After declaring an object, you need to allocate memory for the object. This process is also called instantiating an object. Use the keyword new to instantiate objects in Java.
Syntax format:

Object name = new Construction method name(parameter list]);

Object name: required; used to specify the declared object name.
Parameter list: optional parameter, used to specify the entry parameters of the construction method. If the construction method has no parameters, it can be omitted.

redApple = new Apple;

In addition, you can declare and create objects at the same time

Apple redApple = new Apple();

2. Use of objects:

After creating an object, you can access the member variable of the object, change the value of the member variable, and call the method of the member object. Access to member variables and calls to member methods are achieved by using the operator ".".
Syntax format:

object.Member variable
 object.Member method()

Example: define a class, create the object of the class, change the value of the object variable, and call the member method of the object:

public class Round {
	final float PI = 3.14159f;
	public float r=0.0f;
	public float getArea() {
		float area = PI*r*r;
		return area;
	public float getCiecumference(float r) {
		float circumference = 2*PI*r;
		return circumference;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Round round = new Round();
		round.r = 20;
		float r = 20;
		float area = round.getArea();
		System.out.println("Area of circle:"+area);
		float circunference = round.getCiecumference(r);
		System.out.println("The circumference of the circle is: "+circunference);



3. Destruction of objects

You don't need to do this manually in Java. The garbage collection mechanism provided by java can automatically judge whether objects are still in use, automatically destroy objects that are not in use, and recover the resources occupied by objects.

Topics: Java Algorithm data structure