- If the parent class has a static member assignment or a static initialization block, the static member assignment and static initialization block are executed.
- If a class has a static member assignment or a static initialization block, the class performs a static member assignment and a static initialization block.
- Assign the class member the initial value (the value of the original type member is a specified value, for example, the int type is 0, the float type is 0.0f, The boolean type is false, and the object type's initial value is null)
- If this () call exists in the constructor (it can be another this () call with parameters), execute it. After the execution is complete, proceed to step 1, if this is not called, proceed to the next step. (There may be recursive calls to other constructor methods)
- Execute an explicit super () call (which can be another super () call with parameters) or an implicit super () call (default constructor ), this step further enters the construction process of the parent class and continues to push to the construction of the object.
- Assign values and initialize blocks to Members in the execution class declaration.
- Execute other statements in the constructor.
Step 1 is troublesome, because this call will actually call another constructor of the class, and eventually it should be a constructor of the execution class. It may display the call of super, however, no matter whether super is called or not, super is executed, that is, the constructor of the parent class is always recursive to the object. Therefore, in the construction of sub-classes and parent classes, the first construction or execution is the construction of the parent class, but it is called by the constructor of the subclass, prior to the content in the method body of the constructor, which is determined by the compiler. So I feel that some simple and intuitive expressions should be:
- If the parent class has a static member assignment or a static initialization block, the static member assignment and static initialization block are executed.
- If a class has a static member assignment or a static initialization block, the class performs a static member assignment and a static initialization block.
- Assign the class member the initial value (the value of the original type member is a specified value, for example, the int type is 0, the float type is 0.0f, The boolean type is false, and the object type's initial value is null)
- Execute the constructor and recursively call this (). Finally, execute the constructor of the parent class and recursively execute the constructor of the object.
- After the constructor of the parent class is executed, assign values to the members in the execution class declaration and initialize the blocks.
- Execute other statements in the constructor.
The simplified version is as follows:
- Assignment of static members and static blocks of the parent class
- Static member and static block of subclass
- Parent class Constructor
- Assignment and initialization of the members of the parent class
- Other statements in the constructor of the parent class
- Assign values and initialize blocks to subclass members
- Other statements in the subclass Constructor