---restore content starts---
Occasionally turned to the Java object Initialization order of the face question, re-review a bit, by the way take notes.
1. Precedence of the parent subclass constructor execution
Public classTest2 { Public intField; //Step 1 PublicTest2 () {Field= 1; }}/*************************************************//*** The purpose of the secondary class is only to verify the order in which the parent class subclass constructor executes*/ Public classTest3extendstest2{//Step 2 PublicTest3 () {Field= 2; } Public Static voidMain (string[] args) {Test3 t3=NewTest3 (); System.out.println (The value of the field in the object T3 is "" + T3. Field + "" "); /************************************ * Output Results * The value of the field in the object T3 is "2" \*********************************** */ }}
Conclusion: An object is instantiated, and the order of execution of the construct is the order of the parent class to the subclass, i.e. Test2->test3
2. Constructor initialization and inline initialization
/** * This type of purpose is only to verify inline initialization of fields and the order in which the fields are initialized in the constructor * the inline initialization of inline means that the initialization is assigned at the same time as the field is declared. */public class Test1 {public int Field = 1;//step 1 /** * Step 2 * /Public Test1 () { Fiel D = 2; } public static void main (string[] args) { Test1 t1 = new Test1 (); System.out.println (the value of the field in the object T1 is "" + T1. Field + "" "); /************************************ * Output Results * The value of the field in the object T1 is "2" \************************************/ }}
Conclusion: Instantiating an object first executes the code of the field inline, and then executes the content inside the constructor
3. static block and inline mode (static) initialization
Public classTest4 { Public Static intField = 1;//Step 1 /*** Step 2*/ Static{Field= 2; } Public Static voidMain (string[] args) {System.out.println (Fields field value is "" + Field + "" "); /************************************ * Output result * Field value is "2" \*********************************** */ }}
Conclusion: Inline initialization of static fields is preceded by the use of static blocks to initialize the fields
4. Initializing blocks and initialization blocks (static)
Public classTEST5 { Public Static intField; //Initialize block (static) Static{Field= 1; } //Initialize block{Field= 2; } Public Static voidMain (string[] args) {System.out.println (Fields field value is "" + Field + "" "); /************************************* * Output result * Field value is "1" \********************************** ***/ }}
Conclusion: Initialization blocks should take precedence over static initialization block Execution (Error conclusion)
Always feel that this conclusion is strange, and then looked up some information, that the initialization block will only be executed when an object is instantiated
Public classTEST5 { Public Static intField; //Initialize block (static) Static{Field= 1; } //Initialize block{Field= 2; } Public Static voidMain (string[] args) {System.out.println ("The value of field is" "+ Field +" "Before the object is instantiated); TEST5 Test5=NewTest5 (); System.out.println ("After the object has been instantiated, the value of field is" "+ Field +" ""); //to verify that the initializer is called every time the instantiated objectField = 3; System.out.println ("Assign To field fields, field value is" "+ Field +" ""); TEST5 Test5_0=NewTest5 (); System.out.println ("After instantiating the object again, the value of field is" "+ Field +" ""); /************************************* * Before the object is instantiated, the value of field is "1" * After the object has been instantiated, the value of field is "2" * Assigned To field fields, The value of field is "3" * After instantiating the object again, the value of field is "2" \*************************************/ }}
To verify that the red font mentioned above is partially correct, the value of field is deliberately assigned to 3 and the object is instantiated again
Conclusion: The initializer executes only when instantiated, and each instantiation executes, and the static initializer executes before the initializer
5. Question One
Public classTest6 { Public Static inti =TEST7.J; {i++; } Static{i=0; } PublicTest6 () {i++; }}/*********************************************/ Public classTest7 extends Test6 { Public Static intj =test6.i; Static{J++; } {J++; }}/********************************************/ Public classMain { Public Static voidMain (string[] args) {Test7 T7=NewTest7 (); System. out. println ("The value of the field I is ""+ test6.i +"""); System. out. println ("The value of the field J is ""+ TEST7.J +"""); }}6. Face question Two
Public class Test6 {public staticinti =TEST7.J; {i++; } static {I= 0; } public Test6 () {i++; }}/*********************************************/Public class Test7 {public staticintj =test6.i; static {J++; } {J++; }}/*********************************************/Public class Main {public staticvoidMain (string[] args) {Test7 T7=NewTest7 (); System.out.println ("The value of field I is" "+ test6.i +" ""); System.out.println ("The value of field J is" "+ TEST7.J +" ""); }}
5 and 6 Summary content is two face questions, interested Bo friends can do a bit and then in the comments to reply to two questions of the input content
Java Object Initialization sequence (i)