Order of initialization:
1. Initialize the object memory space to binary 0 (all data members are set to the default value)
2. If the class has a base class, initialize its base class (call the default base class constructor, or you can specify a constructor in the subclass constructor that calls the base class)
3. Static members and static domains (who are first initialized, and only a single space in memory, different objects of the same class can be shared)
4.main method
5. Non-static member variable, reference, instance initialization initialized
6. Constructors
1,2,3,4 is the class loading process, 5,6 is the new object procedure, and only new executes.
classa {a () {System.out.println ("Base class constructor"); System.out.println ("Proof data member is set as default"); Draw (); } A (String s) {System.out.println (s); } voidDraw () {System.out.println ("A.draw ()"); }} Public classTestextendsA {inti = 5; Test () {System.out.println (Sub-class constructor); System.out.println ("proves that the data member was initialized before the constructor call"); Draw (); } String b= "non-static member"; A A=NewA (b + "and non-static reference"); {A B=NewA ("Instance initialization"); } String C= "non-static member and instance initialization peer, who initialized before first"; A D=NewA (c); voidDraw () {System.out.println ("I =" +i); } StaticA start =NewA ("---class loading---"); StaticString e = "static Member"; StaticA f =NewA (E + "and Static Reference")); Static{A g=NewA ("Static domain"); } StaticString h = "static member vs. static domain, who initialized first"; StaticA j =NewA (h); Public Static voidMain (string[] args) {System.out.println ("Main Execution"); System.out.println ("---Create the test object---"); Test k=NewTest (); }}
Output Result:
---class loads---static members and static references to static domain static members with static domain peers, who first initialize who main executes ---Create the test object---= 0= 5
Initialization Order of Java