The class initialization process for Java:
1. Static member variable of parent class, static statement block.
2. Static member variable of subclass, static statement block.
3. Ordinary member variables of the parent class, constructors.
4. Normal member variables of subclasses, constructors.
Note: If the following sentence is not understood, it's OK, look at one example below to understand ~
If the subclass overrides the member function of the parent class, and the member function is called in the constructor in procedure 3 above, then the member function of the subclass is called, and if the member function also contains a normal member variable that is not initialized in the subclass, the int type is initialized to 0 by default, and if the object type is Default is initialized to null.
The Java instance is as follows:
Abstract classBase { Public intAge = GetNumber (100); Static intSage = GetNumber (50); Static{System.out.println ("Base static Block"); } PublicBase () {System.out.println (age); System.out.println ("Base Start"); Draw (); System.out.println ("Base End"); } Static intGetNumber (intbase) {System.out.println ("Base.getnumber int" +base); returnBase; } Public voidDraw () {System.out.println ("Base.draw"); }} Public classInitializeorderextendsBase { Public intAge = GetNumber (1001); Private int_radius = GetNumber (10); Static intSage = GetNumber (250); Static{System.out.println ("Subclass Static Block"); } PublicInitializeorder (intradius) {_radius=radius; System.out.println (age); System.out.println ("Initializeorder initialized"); } Public voidDraw () {System.out.println ("Initializeorder.draw" +_radius); } Public Static voidMain (string[] args) {NewInitializeorder (100); }}
Output Result:
Staticstatic blockbase.getnumber int100base startinitializeorder.draw0 base endbase.getnumber int1001base.getnumber int101001initializeorder initialized
Pay particular attention to the red part of the above output!
A topic that reflects the class initialization process of Java