meaning
Constructors are an important way to create objects. For java classes, at least one constructor must be included.
(if the programmer does not write any constructor in a class, the system will automatically provide a parameterless constructor for the class by default)
effect
The object is initialized at the same time as it is created.
establish
The constructor provided by the system by default is parameterless and the execution body is empty, which is about the same.
public class girl { private String name; private boolean isSimple; private float height; public girl(){} }
The default constructor provided by this system will be initialized by default when creating objects, initializing numerical instance objects to 0, initializing Boolean instance variables to false, and initializing all reference types (including string and char types ~) to null.
In order to change the default initialization of this system and initialize the object to the value we want when creating the object, we need to write our own constructor.
It looks like this.
public class girl { private String name; private boolean isSimple; private float height; public girl(int height,String name,boolean a) { this.name = name; this.isSimple = a; this.height = height; } }
Note that after you define a constructor in a class, the system will no longer automatically default to the constructor you provide. (the system is only a "remedy")
Lovely appearance of constructor: public class name (parameter) {}
Think about it. Why use Public?
Because Public is used for qualification, it can create objects of classes anywhere in the system.
heavy load
So if you are greedy and both are good, what should I do if I want both?
It's very simple. Create it yourself and write a default constructor yourself~
It looks like this
public class girl { private String name; private boolean isSimple; private float height; public girl(){}; public girl(int height,String name,boolean a) { this.name = name; this.isSimple = a; this.height = height; } }
Accordingly, we can also write and add other constructors (after all, how can girls only have one or two construction methods o( ̄)  ̄) o)
Just like function overloading, changing the number and type of parameters can get a new constructor, which is the overload of constructor.
use
Having finished creating the constructor, let's take a look at how we use the constructor to create objects of the corresponding class. Then you need to use the new keyword.
The constructor cannot be called directly. The constructor must use the new keyword to call.
It looks like this
public static void main(String[] args) { girl love = new girl(1.68,Fiona,true); }
Let's have a deep understanding of this sentence.
When calling the constructor with the new keyword, the system will allocate memory space for the object and initialize the object by default. After these operations are completed, the internal statements of the constructor will be executed. In other words, the system has created an object before executing the constructor's executor, but this object cannot be accessed by external programs and can only be referenced through this inside the constructor. After executing the relevant initial assignment statement, this object will be returned as the return value of the constructor. By assigning a value to a variable of another reference type, the object can be accessed by an external program.
Reflected in the above example, the system first creates the girl object and allocates the corresponding memory space, then executes the statements inside the constructor to initialize and assign the value, and then returns the object to the defined reference variable love. (that is, let love point to this memory space)
especially
Think about it. When the constructor is overloaded, what if the statement in the constructor to be added completely contains the statement of the previous constructor? As shown below:
Of course, you can rewrite one side, but it's too stupid o(╥﹏╥) O
The high color value is written as follows (see the notes):
public class girl { private String name; private boolean isSimple; private float height; private int weight; public girl(){}; public girl(int height,String name,boolean a) { this.name = name; this.isSimple = a; this.height = height; } public girl(String name,boolean aa,int h,int w) { this(h,name,aa);//Call the previous constructor for initialization. this.weight = w; } }
Use the this keyword to call the corresponding constructor.
So think about whether you can use the new keyword to create the constructor before calling?
No, because calling new will cause the system to re create an object and initialize that object instead of the object of the constructor itself, so it cannot. Therefore, java provides this keyword to call the corresponding constructor, which can be used by calling the previously defined constructor without re creating a java object.