Learn the idea of Java programming--class initialization p146
The order in which classes are loaded
* 1 Loader startup find Xxx.class file, search base class by extends keyword, load base class first
* Class 2 initialization initializes static member variables and static--->
* 2 Initialize the static member variable and static of the parent class first
* 3 Re-initializes the static member variable and static for this class
* After class loading, object creation begins
* 1 Load the non-static member variable of the parent class first (static member variables are loaded when the class is initialized, and non-static member variables are initialized with the creation of the object)
* 2 Load the constructor of the parent class first
* 3 Reload non-static member variables of this class
* 4 Load the constructor of this class again
*
Overall:
*--Indicates the order
* Parent Class--child class
* Static---Non-static
* Class-to-Object
* Static loads with class loading
* Non-static member variables are loaded with the creation of the object
* Member variables are loaded before the constructor
1 Packagecom.test.java.classs;2 3 /**4 * Created by Administrator on 2015/12/8.5 * Load order of Classes6 * 1 Loader startup find Xxx.class file, search base class by extends keyword, load base class first7 * Class 2 initialization initializes static member variables and static--->8 * 2 Initialize the static member variable and static of the parent class first9 * 3 Re-initializes the static member variable and static for this classTen * After class loading, object creation begins to load One * 1 Load the non-static member variable of the parent class first (static member variables are loaded when the class is initialized, and non-static member variables are initialized with the creation of the object) A * 2 Load the constructor of the parent class first - * 3 Reload non-static member variables of this class - * 4 Load the constructor of this class again the * - * Overall: - * --Indicates the order - * Parent Class--child class + * Static---Non-static - * Class -to-object + * static loads with class loading A * Non-static member variables are loaded with the creation of the object at * Member variables are loaded before the constructor - * - */ - Public classClassloadorderextendsfather{ - //2 The static domain of the child class is loaded after the static member variable of the parent class is loaded - Private Static intK = Printint ("Child static K initialized"); in //5 Initialization of non-static member variables for subclasses - Private intm = printint ("Child non-static variable load"); to + //the constructor of the child class is loaded - PublicClassloadorder () { theSystem.out.println ("Child constructor initialized"); *System.out.println ("k=" +k); $System.out.println ("j=" +j);Panax Notoginseng } - the Static { +SYSTEM.OUT.PRINTLN ("Child static Initialized"); A } the Static intPrintInt2 () { +SYSTEM.OUT.PRINTLN ("Child static function initialized"); - return50; $ } $ - Public Static voidMain (string[] args) { -Classloadorder C =NewClassloadorder (); the } - }Wuyi the classfather{ - Private intI=9; Wu protected intJ; - //4 Parent class constructor loading About Father () { $System.out.println ("Father constructor initialized"); -System.out.println ("i=" +i+ ", j=" +j); -j=39; - } A //3 When an object is created, initialize the non-static member variable of the parent class first + intn = printint ("Father non-static variable loading"); the //1 Load the static domain of the parent class first - Static { $System.out.println ("Father Static initialized"); the } the //1 the Private Static intX1 = Printint ("Father Static. X1 Initialized"); the Static intPrintint (String s) { - System.out.println (s); in return47; the } the}
Results:
Father Static initialized
Father Static. X1 Initialized
Child static k initialized
Child static initialized
Father non-static variable loading
Father Constructor initialized
I=9,j=0
Child non-static variable loading
Child constructor initialized
k=47
j=39
Initialization of Java classes and the order in which objects are created