Source: http://blog.csdn.net/andrew323/article/details/4665379
Below we have two examples to illustrate the object instantiation process.
Example 1:
Compiling and running the program will have the following output
Static Block
Employee Company:china Soft
Non-static Block
Employee phone:0755-51595599
Employee (String)
Empoloyee ()
Let's analyze the results:
1 when instantiating an object in line 34, assign a memory space to the static variable and initialize it, then execute the static block.
Therefore, the output will be here:
Static Block
Employee Company:china Soft
The output of the Employee Company:china Soft also indicates that the static variable is initialized before the static block is executed, or company will be null.
2 then allocates the memory space for the employee in memory and does the default initialization (that is, all variables are initialized to the default values, which are initialized to null).
3 initialization is started when the default initialization is complete. That is, the 5th line is executed, the phone is initialized with "0755-51595599" and the non-static method block is executed, so here is the following output:
Non-static Block
Employee phone:0755-51595599
4 The default constructor is called at the end, and the constructor with parameters is called in the default constructor, so here is the output: Employee (String) in the constructor with the parameter, before the output is Empoloyee ().
Example 2:
In the code above, the manager inherits the previously written employee class, and when we compile and run the manager class, we produce the following output:
Static Block
Employee Company:china Soft
Sub Static Block
Manager Department:sale
Non-static Block
Employee phone:0755-51595599
Employee (String)
Empoloyee ()
Sub non-static Block
Manager salary:8000
Manager (String)
Manager ()
Here we analyze the results:
1 when instantiating an object in line 34, the manager inherits the employee, starting with the parent class employee, assigning the memory space to the parent class static variable and initializing it, and then executing the parent class static block. Then allocate the memory space for the subclass static variable and initialize it, then execute the subclass static block, so we get the following output :
Static Block
Employee Company:china Soft
Sub Static Block
Manager Department:sale
2 then allocates memory space for the parent class employee in memory, initializes it by default, allocates memory space for the subclass manager, and makes default initialization.
3 after the default initialization is complete, the initialization is displayed from the parent class and the non-static method blocks and constructors are executed, and then the subclass begins to display initialization and execution of non-static method blocks and constructors. Therefore, the following output is generated:
Employee phone:0755-51595599
Employee (String)
Empoloyee ()
Sub non-static Block
Manager salary:8000
Manager (String)
Manager ()
Summing up the above, you can get the object initialization process:
1 If there is an inheritance relationship, the post-class subclass of the Father;
2 If there are static variables and static blocks within the class, then static and non-static, and finally the constructor;
3 in an inheritance relationship, the subclass must be initialized after the parent class is initialized.
Initialization process for Java objects