Detailed construction method

Article Directory:

What is the construction method? The role of the construction method?

Definition requirements for construction methods

Overload and overload effect of construction methods

Special application of construction methods: anonymous objects

Construction methods and this keyword

Construction methods and super keywords

What is the construction method? The role of the construction method?

There are properties and methods in the class, and methods are divided into general methods and construction methods.

The construction method is used when the object is instantiated, the main function is to complete the object's property initialization function. The common method is to use after the object is instantiated.

Definition requirements for construction methods

1. The name of the construction method should be the same as the class name.

2. The construction method does not allow return value type declarations.

It is worth noting that:

1. If you do not define a construction method, the class will automatically generate a construction method with no parameters and no return values, as shown below.

2. If you define a construction method, the class will no longer provide a parameterless construction method, which is prone to errors in subclass inheritance, as described below.

Overload and overload effect of construction methods

The construction method is also the method, which can be overloaded.

An example of overloading is as follows:

Why overload?

Overloads can assign values to attributes of objects while instantiating them.

Incidentally, overload construction methods are best arranged in terms of parameters from fewer to more specifications.

Special application of construction methods: anonymous objects

The example is illustrated by the code.

package test;

//Define a class
public class Father {

	private String name;
	private int age;
	
	//Parameterless construction method
	public Father() {
		
	}
	
	//Construction method overload
	public Father(String name) {
		this.name = name;
	}
	
	//Construction method overload
	public Father(String name,int age) {
		this.name = name;
		this.age = age;
	}
	
	//setter, getter methods omitted
	
	//Define a common method
	public void tell() {
		System.out.println("Full name:" + this.name + ",Age" + this.age);
	}
}
package test;

//Main Method Demo
public class JavaDemo {

	public static void main(String[] args) {
		
		//Object pointed to by stack memory reference exists
		Father firstFather = new Father("Zhang San",18);
		firstFather.tell();
		
		//Anonymous object
		new Father("Li Si",19).tell();

	}

}

As the code shows, both normal and anonymous objects use the new keyword to create a space in the heap memory, but anonymous objects do not have a reference to the stack memory, so they can only be used this time, and when they are used, their heap memory space becomes garbage and is waiting to be recycled.

Construction methods and this keyword

this() can be used in the current class's construction methods to call other construction methods in this class, or this.method () can be used to call common methods in this class, as illustrated below.

package test;

public class Father {

	private String name;
	private int age;
	
	public Father() {
		
	}
	
	public Father(String name) {
		this();                            //Look here
        this.name = name;
	}

	public Father(String name,int age) {
		this(name);                        //And here
		this.age = age;
	}
	
	//setter, getter methods omitted

	public void tell() {
		System.out.println("Full name:" + this.name + ",Age" + this.age);
	}

}

Why use this() to call other constructors?

The main purpose is to improve the reusability of executing code in construction methods. Yes, it's just lazy and you can write it again yourself.

It is worth noting that:

1. Be sure to place this() at the beginning of the method.

2. It is not stipulated in this class that other constructors must be called with this(). [Why mention this sentence specifically? Compare the following super()]

Construction methods and super keywords

Look at the code below to see what's interesting.

I called the subclass's construct to instantiate a son, but the parent's construct was also called!

What does that mean? That is, a subclass object must instantiate its parent object before it is instantiated is actually equivalent to hiding a super() in the construction method of its subclass, and of course it must also be the first sentence in the method (stolen by the system).

So the point is here! How can I solve this problem?

1. Place a parameterless construction method in the parent class (combined with what you've learned above, if you've written a parameterless construction method yourself, don't forget to leave a parameterless construction method behind.)

2. If there is no parameterless construction method in the parent class, then let's take the initiative and write super() by ourselves, according to the construction method you defined, such as super (parameter...).

3. To defeat magic with magic, we take the first line and write this(). But in fact, it's just a transfer of contradictions. Other constructions in this class still have to call the parent constructor. So, at least one constructor must be kept as the exit, and this exit must call the parent constructor.

Keywords: Java

Added by RJP1 on Wed, 08 Sep 2021 20:00:41 +0300