Home  >  Article  >  Java  >  Java analyzes what is the instantiation order of classes from the perspective of virtual machine

Java analyzes what is the instantiation order of classes from the perspective of virtual machine

青灯夜游
青灯夜游forward
2018-10-20 15:51:142107browse

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 function, and son(); is the function.

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 function, and then execute the subclass function, in order. Static variable assignment and static code blocks.

2. The constructor is executed in the code, so the function is executed.

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 function): when initializing a class, initialize the parent class first. This is why the function of the parent class is executed before the function of the child class.

2. Function: The compiler automatically collects the assignment actions of all static variables in the class and merges the statements in the static code block according to the order in the source code.

3. function: First call the function of the parent class, and then the compiler automatically collects the assignment operations and construction code blocks of instance variables in the class according to the order in the source code. The statements in are merged and then inserted before the constructor method, and finally the constructor code written by the programmer himself.

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete