The title is a bit of a mouthful, probably meaning that in a Java class, the order in which the domain and constructor methods are called.
1. Cases of no inheritance
In a single class scenario, the initialization order is static data sequentially, the constructor of the inherited base class, the member variable, and the constructor that is called.
Where static data is initialized only once.
Packagecom.khlin.binding.test; Public classAPP2 { Public Static voidMain (string[] args) {son son=NewSon (); }}classSon { PublicSon () {System.out.println ("This is son."); } PublicSon (intAge ) {System.out.println (' Son is ' + age + ' years old '.); } PrivateHeight height =NewHeight (1.8f); Public StaticGender Gender =NewGender (true);}classHeight { PublicHeight (floatheight) {System.out.println ("Initializing height" + height + "meters."); }}classGender { PublicGender (BooleanIsmale) { if(Ismale) {System.out.println ("This is a male."); } Else{System.out.println ("This is a female."); } }}
Output:
2. Status of succession
Modify the code a little bit, add two base classes, let son inherit father, father inherit grandpa.
The case for inheritance is more complicated. Because of inheriting the base class, it also goes back up, recursively calling the base class's parameterless construction method.
In our example, after initializing the static data, we will go back to the default constructor method of father, and then go back to the default constructor of Grandpa.
Note: If you explicitly call the parameter construction method of a parent class in the constructor of a subclass, the JVM invokes the specified constructor method instead of the default constructor method.
Both the base class and the subclass have static data, member variables, and scenarios for constructing methods
We continue to modify the code so that it is eventually rendered as follows:
1 Packagecom.khlin.binding.test;2 3 Public classAPP2 {4 Public Static voidMain (string[] args) {5Son son =NewSon ();6 }7 }8 9 classGrandpa {Ten PublicGrandpa () { OneSystem.out.println ("This is Grandpa.")); A } - - PublicGrandpa (intAge ) { theSystem.out.println (' Grandpa is ' + age + ' years old '.); - } - - PrivateHeight height =NewHeight (1.5f); + - Public StaticGender Gender =NewGender (true, "Grandpa"); + } A at classFatherextendsGrandpa { - - PublicFather () { -System.out.println ("This is father.")); - } - in PublicFather (intAge ) { -System.out.println (' father is ' + age + ' years old '.); to } + - PrivateHeight height =NewHeight (1.6f); the * Public StaticGender Gender =NewGender (true, "Father"); $ }Panax Notoginseng - classSonextendsFather { the + PublicSon () { A Super(50); theSystem.out.println ("This is son.")); + } - $ PublicSon (intAge ) { $System.out.println (' son is ' + age + ' years old '.); - } - the PrivateHeight height =NewHeight (1.8f); - Wuyi Public StaticGender Gender =NewGender (true, "Son"); the } - Wu classHeight { - PublicHeight (floatheight) { AboutSystem.out.println ("initializing height" + height + "meters."); $ } - } - - classGender { A PublicGender (BooleanIsmale) { + if(Ismale) { theSystem.out.println ("This is a male.")); -}Else { $System.out.println ("This is a female.")); the } the } the the PublicGender (BooleanIsmale, String identify) { - if(Ismale) { inSYSTEM.OUT.PRINTLN (Identify + "is a male."); the}Else { theSYSTEM.OUT.PRINTLN (Identify + "is a female."); About } the } the}
What will the final output be?
Refer to the analysis of another case below. Links: http://bbs.csdn.net/topics/310164953
In our example, the load order should look like this:
Grandpa Static Data
Father static data
Son static data
Grandpa Member variables
Grandpa Construction Method
Father member variables
Father Construction Method
Son member Variable
Son Construction Method
So the output is as follows:
Java class loading and invocation order of instantiation