Java constructor or constructor

Reprinted from blog Park LeeAaron

Definition of construction method

Constructors are also called constructors or constructors

The construction method is the same as the class name. There is no return value. You can't even write void

The construction method can be overloaded (overloaded: the method name is the same and the parameter list is different)

If there is no constructor in a class, the compiler adds a default constructor to the class.

The default construction method format is as follows:

public class name (){

}

If you add a constructor manually, the default constructor disappears.

It is recommended to write out the parameterless constructor in the code.

public class Student {

    public String name;
    public int age;
    
    public void eat() {
        System.out.println("eat....");
    }
    
    //constructor 
    /**
     * The name is the same as the class name. There is no return value. void cannot be written
     * Overload constructor can
     * If a constructor is not manually added to the class, the compiler will add another parameterless constructor by default
     * If you manually add a constructor (whatever form it takes), the default constructor disappears
     */
    public Student() {
        System.out.println("Parameterless constructor");
    }
    
    public Student(int a) {
        System.out.println("Constructor of one parameter");
        age = 15;
    }
    
    public Student(int a, String s) {
        System.out.println("Constructor with two parameters");
        age = a;
        name = s;
    }
}

 

Function of construction method

The constructor is called when the object is created. The specific call is determined by the parameters.

The constructor is used to assign initial values to the member variables of the object being created.

public class Test {

    public static void main(String[] args) {
        
        //Call parameterless constructor
        Student s1 = new Student();
        //Calling a parameterized constructor
        Student s2 = new Student(15);
        System.out.println(s2.age);
        Student s3 = new Student(34, "Xiao Ming");
        System.out.println(s3.name + ":" + s3.age);
    }

}

 

The use of this construction method

The constructor can use this to represent the object just created

this construction method can be used for

this access object properties

this access instance method

this calls the overloaded other constructor in the constructor (to avoid falling into an endless loop)

Can only be in the first row

The creation of new objects is not triggered

public class Student {

    public String name;
    public int age;
    
    public void eat() {
        System.out.println("eat....");
    }
    //constructor 
    //Using this() to call overloaded constructors cannot call each other at the same time to avoid falling into an endless loop
    public Student() {
        //this() must appear in the first line of the constructor and will not create a new object
        this(15);//A constructor with an int type parameter was called
        System.out.println("default constructor ");
    }
    public Student(int a) {
        this.eat();
        eat();//this. Can be omitted
    }
    //this represents the object just created in the constructor
    public Student(int a, String s) {
        System.out.println("Constructor with two parameters");
        this.age = a;
        this.name = s;
    }
}
public class Test {

    public static void main(String[] args) {
        Student s1 = new Student(15, "Xiao Ming");
        System.out.println(s1.name + ":" + s1.age);
        Student s2 = new Student(12, "Xiao Hong");
        System.out.println(s2.name + ":" + s2.age);
        
        Student s3 = new Student();
    }
}

 

Summarize the function of this in case method and construction method

this is one of the manifestations of java polymorphism

this can only exist in constructor methods and instance methods, and cannot appear in static modified methods or code blocks

this represents the object just created in the constructor

this represents the object that calls the modified method in the instance method

this can access object properties and instance methods in instance methods and constructor methods

this can sometimes be omitted

this can be used as a return value in an instance method

this can be used as an argument

this calls the overloaded constructor

Posted by irish21 on Sat, 14 May 2022 14:55:57 +0300