Java class initialization order, java class initialization order
In java, when we create an object, there is a certain order of loading the members, initialization blocks, and constructor of the object. See the following figure:
I. initialization sequence of a single class (without a base class:
Public class Parent {
Static String staticStr = "parent static variable ";
Private String str = "parent class member variable ";
Static {
System. out. println (staticStr );
System. out. println ("parent class static initialization block ");
}
{
System. out. println (str );
System. out. println ("parent class initialization block ");
}
Public Parent (){
System. out. println ("parent class structure ");
}
Public static void main (String [] args ){
New Parent ();
// New Parent ();
}
}
Output: static variables of the parent class
Static initialization block of the parent class
Parent class member variable
Parent class initialization Block
Parent Class Construction
/* If new Parent () is released on the second line, the following content will be added to the output:
Parent class member variable
Parent class initialization Block
Parent Class Construction
*/
Single class sequence: ① static (Member, block)> non-static (Member, block)> Construction
② The Order of members and blocks depends on "The front and back positions of the members and blocks they define in the class, and who is first initialized ".
③ Static members or blocks belong to the class. Therefore, they are initialized first and only once.
④ The structure is finally executed.
Ii. initialization sequence of inheritance classes
Class SubClass extends Parent {
Static String staticStr = "subclass static variables ";
Private String str = "subclass member variable ";
Static {
System. out. println (staticStr );
System. out. println ("subclass static initialization block ");
}
{
System. out. println (str );
System. out. println ("subclass initialization block ");
}
Public SubClass (){
System. out. println ("subclass structure ");
}
Public static void main (String [] args ){
System. out. println ("subclass main method ");
New SubClass ();
}
}
Output: static variables of the parent class
Static initialization block of the parent class
Static subclass Variables
Static initialization block of subclass
Subclass main method
Parent class member variable
Parent class initialization Block
Parent Class Construction
Subclass member variable
Subclass initialization Block
Subclass Construction
Inheritance class sequence:
① Static parent class (Member, block)> static subclass (Member, block)> non-static parent class (Member, block)> parent class structure> non-static subclass (member, block)> subclass Construction
② Similarly, the order of members and blocks is the same as that in a single class.
③ A child has a parent, but the static (member and block) of the Child class is prior to the non-static (member and block) of the parent class, because the static class belongs to the class. For non-static and constructor, naturally, the parent class is fully loaded and then the Child class is loaded.
Follow ginger to talk about technology, no.: helojava, or scan the following QR code.
One post per day, chicken soup for technical purposes.