package Study.Initialization;class Base{private String str1=init("str1");private String str2=init("str2");public static String str3=init("static str3");public Base(String str1,String str2){System.out.println("Run Construction Base");this.str1=str1;this.str2=str2;}protected static String init(String str){System.out.println(str);return str+" init";}}class Derived extends Base{ private String str4=init("str4"); public static String str5=init("static str5"); public Derived(String str1,String str2,String str4){ super(str1,str2); System.out.println("Run Construction Derived"); this.str4=str4; }}public class extendInitialization {public static void main(String[] args) {System.out.println("run extendInitialization.main()");Derived d=new Derived("construction str1","construction str2","construction str4"); System.out.println("----------------------------------------");Derived d2=new Derived("construction str11","construction str22","construction str44"); }}
Running result:
Run extendinitialization. Main ()
Static str3
Static str5
Str1
Str2
Run Construction Base
Str4
Run construction derived
----------------------------------------
Str1
Str2
Run Construction Base
Str4
Run construction derived
Summary:
The initialization sequence of derived:
1. initialize static members of the base class in the defined sequence
2. initialize static members of the extension class in the defined sequence
(The above two items are run when the extension class object is defined for the first time)
3. Perform the specified initialization of the base class in the defined sequence
4. Execute the constructors of the base class
5. Execute the specified initialization of the extension class in the defined sequence.
6. constructor of the row extension class