Constructors are special methods in classes that initialize instance variables of classes and are automatically invoked after creating objects (new operators).
The Java constructor has the following characteristics:
-
The construction method name must be the same as the class name.
-
The constructor has no return value, including void.
-
The constructor can only be used in conjunction with the new operator.
The sample code is as follows:
1 //Rectangle.java file 2 package com.a51work6; 3 4 // Rectangle class 5 public class Rectangle { 6 7 // Rectangle width 8 int width; 9 // Rectangle height 10 int height; 11 // Rectangle area 12 int area; 13 14 // Construction method 15 public Rectangle(int w, int h) { 16 width = w; 17 height = h; 18 area = getArea(w, h); 19 } 20 ... 21 }
Line 15 declares a constructor with two parameters w and h to initialize the two member variables width and height of the Rectangle object, noting that there are no previous return values.
Default constructor
Sometimes you don't see any constructions in the class at all. For example, the User class code in this section is as follows:
1 //User.java file 2 package com.a51work6; 3 4 public class User { 5 6 // User name 7 private String username; 8 9 // User password 10 private String password; 11 12 }
From the above User class code, there are only two member variables and no construction method can be seen, but we can call the parametric constructor to create the User object, because Java Virtual Opportunity provides a default constructor without parameters for classes without constructors. The default constructor has no statements in its method body. The default constructor is equivalent to the following code:
//Default constructor public User() { }
There are no statements in the method body of the default constructor, and it is impossible to initialize member variables. Then these member variables will use default values, which are related to the data type.
Construction method overload
There can be several constructions in a class, which have the same name (same as the class name) and different parameter lists, so they must be overloaded.
The construction method overloads the sample code as follows:
1 //Person.java file 2 package com.a51work6; 3 4 import java.util.Date; 5 6 public class Person { 7 8 // Name 9 private String name; 10 // Age 11 private int age; 12 // Date of birth 13 private Date birthDate; 14 15 public Person(String n, int a, Date d) { 16 name = n; 17 age = a; 18 birthDate = d; 19 } 20 21 public Person(String n, int a) { 22 name = n; 23 age = a; 24 } 25 26 public Person(String n, Date d) { 27 name = n; 28 age = 30; 29 birthDate = d; 30 } 31 32 public Person(String n) { 33 name = n; 34 age = 30; 35 } 36 37 public String getInfo() { 38 StringBuilder sb = new StringBuilder(); 39 sb.append("Name: ").append(name).append('\n'); 40 sb.append("Age: ").append(age).append('\n'); 41 sb.append("Date of birth: ").append(birthDate).append('\n'); 42 return sb.toString(); 43 } 44 }
The above code Person class code provides four overloaded construction methods, if there is accurate name, age and birth date information, you can choose the construction method of line 15 to create Person object; if only name and age information, you can choose the construction method of line 21 to create Person object; if only name and birth date information, you can choose line 26 of code. The constructor creates the Person object; if only the name information is available, the constructor in line 32 of the code can be used to create the Person object.
Tips: If a constructor with parameters is added to the class, the system will not automatically generate a constructor without parameters, so it is recommended to add a constructor with parameters and then manually add a default constructor without parameters.
Construction method encapsulation
Constructors can also be encapsulated. The access level of constructors is the same as that of common methods, and the access level reference of constructors is the same. Java Encapsulation and Access Control As shown in the figure above. The sample code is as follows:
1 //Person.java file 2 package com.a51work6; 3 4 import java.util.Date; 5 6 public class Person { 7 8 // Name 9 private String name; 10 // Age 11 private int age; 12 // Date of birth 13 private Date birthDate; 14 15 // Public Level Restrictions 16 public Person(String n, int a, Date d) { 17 name = n; 18 age = a; 19 birthDate = d; 20 } 21 22 // Default level limit 23 Person(String n, int a) { 24 name = n; 25 age = a; 26 } 27 28 // Protection Level Limitation 29 protected Person(String n, Date d) { 30 name = n; 31 age = 30; 32 birthDate = d; 33 } 34 35 // Private Level Restrictions 36 private Person(String n) { 37 name = n; 38 age = 30; 39 } 40 41 ... 42 }
Line 16 of the above code is a constructor that declares the public level. Line 23 declares the default level, which can only be accessed in the same package. Line 29 of the code is a protection level construct, which is the same as the default level in the same package and can be inherited by subclasses in different packages. Line 36 of the code is a private level constructor, which can only be used in the current class and is not allowed to be accessed outside. Private constructors can be applied to design patterns such as singleton designs.
----------------------------------------------------------------------------
The order of execution of initialization blocks and constructors
Readers can first read the following code on their own to determine the output results:
1 class B extends Object 2 { 3 static 4 { 5 System.out.println("Load B"); 6 } 7 public B() 8 { 9 System.out.println("Create B"); 10 } 11 } 12 class A extends B 13 { 14 static 15 { 16 System.out.println("Load A"); 17 } 18 public A() 19 { 20 System.out.println("Create A"); 21 } 22 } 23 24 public class Testclass 25 { 26 public static void main(String [] args) 27 { 28 new A(); 29 } 30 }
Output results:
Load B
Load A
Create B
Create A
Initialization block is executed before the constructor executes. In class initialization stage, static initialization block of top-level parent class is executed first, then downwards, and finally static initialization block of current class is executed. When creating an object, the construction method of top-level parent class is invoked first, then downwards, and finally the construction method of this class is invoked. So the order of execution is: parent static code - > child static code block - > parent constructor code block - > parent constructor - > child constructor code block - > child constructor