View code
1 Package Com. test; 2 3 Public Class Test1 { 4 5 /** 6 * @ Param ARGs 7 */ 8 Public Static Void Main (string [] ARGs ){ 9 // Todo auto-generated method stub 10 System. Out. println ("------------------ first instantiated object --------------------" ); 11 Parent = New Child (); 12 System. Out. println ("------------------ the second instantiation object --------------------" ); 13 Parent = New Child (); 14 } 15 } 16 17 Class Parent { 18 Public Static Staticvarible =New Staticvarible ("parent class-static variable 1" ); 19 Public Staticvarible instvarible = New Staticvarible ("parent class-member variable 1" ); 20 21 Static { 22 System. Out. println ("parent class-static block" ); 23 } 24 25 { 26 System. Out. println ("parent class-initialization block" ); 27 } 28 29 Public Parent (){ 30 System. Out. println ("parent class-instance constructor" ); 31 } 32 } 33 34 Class Child Extends Parent { 35 Public Static Staticvarible = New Staticvarible ("subclass-static variable 1" ); 36 Public Staticvarible instvarible = New Staticvarible ("subclass-member variable 1" ); 37 38 Static { 39 System. Out. println ("subclass-static block" ); 40 } 41 42 Public Child (){ 43 System. Out. println ("subclass-instance constructor" ); 44 } 45 46 { 47 System. Out. println ("subclass-initialization block" ); 48 } 49 50 } 51 52 Class Staticvarible { 53 Public Staticvarible (string info ){ 54 System. Out. println (Info ); 55 } 56 }
First instantiated object
Parent class-static variable 1
Parent class-static Block
Subclass-static variable 1
Subclass-static Block
Parent class-member variable 1
Parent class-initialization Block
Parent class-instance Constructor
Subclass-member variable 1
Subclass-initialization Block
Subclass-instance Constructor
------------------ The second instantiated object --------------------
Parent class-member variable 1
Parent class-initialization Block
Parent class-instance Constructor
Subclass-member variable 1
Subclass-initialization Block
Subclass-instance Constructor
Conclusion:
From the above results, we can see that when instantiating an object, the loading sequence of each part is as follows:
Parent class static member/parent class static initialization block-> subclass static member/subclass initialization block-> parent class member variable/parent class initialization block-> parent class constructor-> subclass Member variable/subclass initialization block-> subclass Constructor
StaticCodeBlock and static members are called only once when the class is loaded into the memory for the first time.
Non-static code blocks and members. Each time an object is created, it is called before the constructor.
Constructor, called at the end of each object creation.
Create a parent class object before creating a subclass object.