The class initialization process, the execution sequence of the instance initialization process code
First, there is the following code:
public class ClassTest {
public static void main(String[] args) {
Son s1=new Son();
System.out.println();
Son s2=new Son();
}
static {
System.out.println("(11)main方法所在的类");
}
}
class Father{
private int i=test();
private static int j=method();
static {
System.out.println("(1)父类中静态代码块");
}
Father(){
System.out.println("(2)父类构造方法");
}
{
System.out.println("(3)父类代码块");
}
public int test() {
System.out.println("(4)父类成员方法-赋值语句");
return 1;
}
public static int method() {
System.out.println("(5)父类静态成员方法-赋值语句");
return 1;
}
}
class Son extends Father{
private int i=test();
private static int j=method();
static {
System.out.println("(6)子类中静态代码块");
}
Son(){
System.out.println("(7)子类构造方法");
}
{
System.out.println("(8)子类代码块");
}
public int test() {
System.out.println("(9)子类成员方法-赋值语句");
return 1;
}
public static int method() {
System.out.println("(10)子类静态成员方法-赋值语句");
return 1;
}
}
Please write the results of the above code.
Problem analysis:
First of all, we can see that we have defined a Father class and its subclass Son class. The structure of their classes is roughly the same. There are static code blocks, construction methods, code blocks, member methods, static member methods , and requirements. Output their order of execution.
Execution result:
that is:
the first instantiation of the subclass:
- Parent class static member method -> Parent class static code block -> Subclass static member method -> Subclass static code block -> Subclass private member method
- ->Parent class code block->Parent class construction method->Subclass private member method->Subclass code block->Subclass construction method
- Second instantiation:
- Subclass member method->parent class code block->parent class construction method->subclass member method->subclass code block->subclass construction method
Class initialization process : (5) (1) (10) (6) process, (11) temporarily excluded
- To create an instance of a class, you need to load and initialize the class first.
- Note: The class where the main method is located needs to be loaded and initialized first (so 11 is executed first)
- To initialize a subclass, you need to initialize the parent class first (so the Father class will be initialized first when the new Son class is first)
- The class initialization will call the clinit() method:
-
由静态类变量显示赋值代码和静态代码块组成 只执行一次
-
赋值代码和静态代块的执行顺序会由它们的位置决定
- The clinit method is executed only once.
Example initialization process: (9)(3)(2)(9)(8)(7) process
- There may be more than one init() method called, and there are several init() methods for several constructors
- It is composed of non-static instance variable display assignment code and non-static code block
- super()->non-static assignment->non-static code block->parameterless constructor, test() will overwrite the code in the parent class, and static methods cannot be rewritten.
Method rewriting: process (9)
- The test method of the parent class inherited by Son actually overrides the test method of the parent class, so i=test() actually executes the method of the parent class overridden by the subclass.
- Static methods cannot be overridden, final modified methods, and private modified subclass invisible methods cannot be overridden.
Polymorphism of the object:
- If a subclass overrides the method of the parent class, the code that is called by the subclass object must be the code that the subclass has rewritten.
- The default calling object for non-static methods is this.