In Methods and Data Members, we mentioned that objects in Java are initialized when they are created. During initialization, the object's data members are assigned initial values. We can initialize it explicitly. If we do not assign an initial value to the data member, the data member will adopt a default initial value based on its type.
Explicit initialization requires us to determine the initial value when writing the program, which is sometimes inconvenient. We can use constructor to initialize objects. Constructors can initialize data members and specify specific operations. These operations are performed automatically when the object is created.
define constructor
A constructor is a method. Like normal methods, we define constructors in the class. Constructors have the following basic characteristics:
1. The name of the constructor is the same as the name of the class
2. The constructor has no return value
We define the constructor of the Human class:
public class Test{ public static void main(String[] args) { Human aPerson = new Human(160); System.out.println(aPerson.getHeight()); }}class Human{ /** * constructor */ Human (int h) { this.height = h; System.out.println("I'm born"); } /** * accessor */ int getHeight() { return this.height; } int height;}
The above program will print the copied code code as follows:
I'm born
160
Constructors can receive parameter lists like ordinary methods. Here, the constructor Human() receives an integer as parameter. In the body of the method, we assign the integer parameter to the data member height. The constructor does two things when the object is created:
Constructors can receive parameter lists like ordinary methods. Here, the constructor Human() receives an integer as parameter. In the body of the method, we assign the integer parameter to the data member height. The constructor does two things when the object is created:
1. Provide initial value for data member this.height = h;
2. Perform specific initial operations System.out.println("I'm born");
In this way, we can flexibly set the initial value when calling the constructor without being as constrained as explicit initialization.
How is the constructor called? When we create classes, we all use new Human(). In fact, we are calling the constructor of the Human class. When we do not define this method, Java will provide a blank constructor to be called when using new. But when we define a constructor, Java will call the defined constructor when creating an object. When calling, we provide a parameter of 160. You can also see from the final running results that the height of the object is indeed initialized to 160.
Initialization method priority
In methods and data members, we can see that if we provide an explicit initial value, then the data member will take the explicit initial value instead of the default initial value. But if we both provide an explicit initial value and initialize the same data member in the constructor, the final initial value will be determined by the constructor. For example, the following example:
public class Test{ public static void main(String[] args) { Human aPerson = new Human(160); System.out.println(aPerson.getHeight()); }}class Human{ /** * constructor */ Human (int h) { this.height = h; } /** * accessor */ int getHeight() { return this.height; } int height=170; // explicit initialization}
The running result is:
Copy the code code as follows:
160
The final initialization value of the object is consistent with the value in the construction method. therefore:
Build methods > Explicit initializers > Default initializers
(In fact, the so-called priority is related to the order of execution during initialization, I will delve into this point later)
Method overloading
A class can define more than one constructor, for example:
public class Test{ public static void main(String[] args) { Human neZha = new Human(150, "shit"); System.out.println(neZha.getHeight()); }}class Human{ /** * constructor 1 */ Human(int h) { this.height = h; System.out.println("I'm born"); } /** * constructor 2 */ Human(int h, String s) { this.height = h; System.out.println("Ne Zha: I'm born, " + s); } /** * accessor */ int getHeight() { return this.height; } int height ;}
Running results:
Copy the code code as follows:
Ne Zha: I'm born, shit
150
Two constructors are defined above, both named Human. The two constructors have different parameter lists.
When using new to create an object, Java will decide which constructor to build based on the provided parameters. For example, when building neZha, we provide two parameters: the integer 150 and the string "shit", which corresponds to the parameter list of the second build method, so Java will call the second build method.
In Java, Java determines the method to be called based on both the method name and the parameter list. This is called method overloading. Build methods can be overloaded, and ordinary methods can also be overloaded, such as the breath() method below:
public class Test{ public static void main(String[] args) { Human aPerson = new Human(); aPerson.breath(10); }}class Human{ /** * breath() 1 */ void breath() { System.out.println("hu...hu..."); } /** * breath() 2 */ void breath(int rep) { int i; for(i = 0; i < rep; i++ ) { System.out.println("lu...lu..."); } } int height;}
Running results:
Copy the code code as follows:
lu...lu...
lu...lu...
lu...lu...
lu...lu...
lu...lu...
lu...lu...
lu...lu...
lu...lu...
lu...lu...
lu...lu...
As you can see, since a parameter is provided during the call: the integer 10, the second breath() method whose parameter list matches it is called.
Summarize
constructor characteristics: same name as class, no return value
Constructor purpose: initialization, initial operation method overloading: method name + parameter list -> which method is actually called