One object in Java is constructed as follows:
1. Load the parent class with the ClassLoader, allocate space for all static variables of the parent class in the order defined by the parent class static variables, and give the parent class A static variable default value.
2. Load yourself with the class loader, allocate space for all of your static variables in the order defined by your own static variables, and give yourself static variable defaults.
3. Assign a defined value to all static variables in the parent class in the order defined by the parent class static variable.
4. Execute the parent class static code block.
5. Assign a defined value to all of your static variables in the order defined by your own static variables.
6. Execute your own static block of code.
7. Allocate space for the parent class instance variable and give the default value.
8. Allocate space for your instance variable and give the default value.
9. Assign a defined value to all instance variables of the parent class in the order defined by the parent class instance variable.
10. Execute the construction code block of the parent class.
11. Execute the constructor method of the parent class.
12. Assign the values defined for all instance variables in the order defined by their instance variables.
13. Execute your own construction code block.
14. Execute your own method of construction.
15. Object construction is complete.
Attention:
1. The parent class, if there is a parent class, initializes the parent class of the parent class in this order until object.
2. If you perform steps 3, 5, 9, and 12 o'clock, the defined value introduces a different class, the class is initialized, and if the referenced class has a referenced class, the reference class of the reference class is initialized in this order until all the referenced classes are initialized.
3. If a class does not have a parent class (such as the object class), its initialization order can be simplified to 2, 5, 6, 8, 12, 13, 14, 15.
Reprint please indicate the original address, Please respect the original, thank you.
The Java object Construction process