The sequence of constructor calls when class initialization:
(1) The storage space of the initialized object is zero or null value;
(2) Call the parent class constructor;
(3) The initialization expressions of class member variables and instance member variables are called sequentially;
(4) Call the constructor itself .
Example:
public class Dollar extends money{
RMB r=new RMB ()
Public Dollar () {
System.out.println ("Dollar is construct!");
}
public static void Main (string[] args) {
New Dollar ();
}
}
Class money{
Public money () {
System.out.println ("Money is construct");
}
}
Class rmb{
Public Rmb () {
System.out.println ("RMB is construct");
}
}
Output Result:
Money is construct
$ is construct
Dollar is construct!
we'll write a program ourselves to test the order of the run:
Output Result:
Parent Class The class member variable description is now run in the following order:
- Parent class static Block
- Self- class member variables
- Self-static block
- Parent class Instance member variable
- Parent Class Block
- Parent class constructor
- Self instance member variables
- Self block
- Self-constructor
Java Initialization constructor Call order