First, background:
In the interview, in the Java basics, the class loading order is often asked, many times we are not sure how the class loading order, then today we will look at the loading sequence with inherited classes in the end what is the matter? In this note also convenient later review consolidation!
Second, the test steps:
1. Parent Class Code
1 PackageCom.hafiz.zhang;2 3 Public classFu4 {5 Private inti = print ("This is Father common variable");6 Private Static intj = Print ("This is Father static variable");7 Static{8System.out.println ("This is Father static code block");9 }Ten { OneSystem.out.println ("This is Father common code block"); A } - PublicFu () { -System.out.println ("This is Father constructor"); the } - - Static intprint (String str) { - System.out.println (str); + return2; - } +}
2. Sub-class Code
1 PackageCom.hafiz.zhang;2 3 Public classZiextendsFu4 {5 Private intL = Print ("This is son common variable");6 Private Static intm = Print ("This is son stati variable");7 Static{8System.out.println ("This is son static code block");9 }Ten { OneSystem.out.println ("This is son common code block"); A } - PublicZi () { -System.out.println ("This is son constructor"); the } - Public Static voidMain (string[] args) { - NewZi (); - } +}
The result of the final operation is:
Let's change the position of static code blocks and static member variables in two classes and rerun
3. Modified Parent Class Code
1 PackageCom.hafiz.zhang;2 3 Public classFu4 {5 Static{6System.out.println ("This is Father static code block");7 }8 {9System.out.println ("This is Father common code block");Ten } One PublicFu () { ASystem.out.println ("This is Father constructor"); - } - the Static intprint (String str) { - System.out.println (str); - return2; - } + Private inti = print ("This is Father common variable"); - Private Static intj = Print ("This is Father static variable"); +}
4. Modified sub-class code
1 PackageCom.hafiz.zhang;2 3 Public classZiextendsFu4 {5 Static{6System.out.println ("This is son static code block");7 }8 {9System.out.println ("This is son common code block");Ten } One PublicZi () { ASystem.out.println ("This is son constructor"); - } - Public Static voidMain (string[] args) { the NewZi (); - } - Private intL = Print ("This is son common variable"); - Private Static intm = Print ("This is son stati variable"); +}
The result of the modified operation:
Third, test results
The test result shows that the program first loads the class and then initializes the class.
The order of the loaded classes is: Load the base class first, and then load the subclass after the base class has finished loading.
The order of initialization is: Initialize the base class first, and then initialize the subclass after the base class is initialized.
Finally, the class loading order is: Initialize the base class static variable and static code block in the order of declaration, then initialize the subclass static variable and static code block in the order of declaration, then initialize the base class normal variable and ordinary code block in the order of declaration, then execute the base class constructor, Then initialize the subclass normal variable and the ordinary code block in the order of declaration, and finally execute the subclass constructor.
The order of execution in this test is to initialize the static variable first, which needs to be loaded before executing the main () method. Then the main method, if new, initializes the basic member variables of the object class (non-Methods), including building blocks of code, which are executed sequentially in writing, and then calling the constructor. With regard to the initialization mechanism of inheritance, first execute the class containing the main method, and observe that the Zi class contains the base class Fu, that is, the static variable of the FU class is loaded first, and then the static variable of the Zi class is loaded. After loading the static variable, call the main () method, and new Zi () initializes the base variable and constructs the block of code, and then calls the constructor of the base class. It then initializes the base variable of the subclass Zi and constructs the block of code, and then executes the constructor of the child class.
Loading sequence with inheriting classes in Java in detail and actual combat