Home >Java >javaTutorial >Java analyzes what is the instantiation order of classes from the perspective of virtual machine
What this article brings to you is Java’s analysis of what is the instantiation order of classes from the perspective of a virtual machine. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. First show the example code (Son.java & Father.java)
public class Father { public static int a=10;//父类的静态变量 static{//父类的静态代码块 a=20; } {//父类的构造代码块 a=30; } public Father() {//父类的构造方法 a=40; } }
public class Son extends Father{ public static int s=10;//子类的静态变量 public int k=20;//子类的实例变量 static{//子类的静态代码块 s=20; } {//子类的构造代码块 s=30; } public Son() {//子类的构造函数 s=40; } {//子类的构造代码块 s=50; } }
2. Compile the son.java file into a son.class file, and then use javap to decompile and view Son The bytecode instructions are used to analyze the loading sequence of Son, which is easier to understand (javap -v -c Son > p.txt).
3. After executing the code "new Son();", analyze the loading order of the classes.
The static{}; below is the
static {}; descriptor: ()V flags: ACC_STATIC Code: stack=1, locals=0, args_size=0 0: bipush 10 2: putstatic #11 // Field s:I--------------------------顺序执行静态变量的赋值 5: bipush 20 7: putstatic #11 // Field s:I--------------------------顺序执行静态代码块 10: return
public packet1020.Son(); descriptor: ()V flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #16 // Method packet1020/Father."<init>":()V--------------------执行父类的<init>函数(顺序不变,第一个) 4: aload_0 5: bipush 20 7: putfield #18 // Field k:I------------------------------------------------按顺序收集实例变量赋值 10: bipush 30 12: putstatic #11 // Field s:I------------------------------------------------按顺序收集构造代码块 15: bipush 50 17: putstatic #11 // Field s:I------------------------------------------------按顺序收集构造代码块 20: bipush 40 22: putstatic #11 // Field s:I------------------------------------------------最后执行自己的构造函数代码(顺序不变,最后一个) 25: return
Start analysis:
1. Trigger the loading of the class. In the initialization phase, first execute the parent class
2. The constructor is executed in the code, so the
Conclusion:
1. Static variable assignments are sequentially executed in the parent class, static code blocks
2. Static variable assignments are sequentially executed in the subclass, static code blocks
3. Sequentially execute instance variable assignments in the parent class and construct code blocks
4. Parent class constructor
5. Sequentially execute instance variable assignments and construct code blocks in the subclass
6. Subclass constructor
Name explanation: Excerpted from "In-depth Understanding of Java Virtual Machine: JVM Advanced Features and Best Practices" by Mr. Zhou Zhiming
1. Yes And there is only the third of the 4 situations where the class must be initialized (execute the
2.
3.
The execution order of the construction code block precedes the constructor function
<init>(){ 1.调用父类<init>方法 2.顺序执行实例变量的赋值操作和构造代码块 3.程序员自己的构造函数方法代码 }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more related tutorials, please visit Java video tutorial, java development graphic tutorial, bootstrap video tutorial!
The above is the detailed content of Java analyzes what is the instantiation order of classes from the perspective of virtual machine. For more information, please follow other related articles on the PHP Chinese website!