JVM loading process of Hello class

1 Example to be parsed

public class Hello {



    private static String name="test";



    private int age = 10;



    public static void main(String[] args) {

        Hello hello = new Hello();

    }

}

2 Loading process

The first class loader first loads the bytecode file Hello.class into the JVM's method area, and Go through the steps of loading, verifying, preparing, parsing, and initializing.

  • Preparation stage: Initialize the class variable, where name is a class variable, so initialize it to null in the preparation phase, such as byte=0, short=0, int=0, long=0L, char=' ', boolean= False, float=0.0f, double=0.0d, reference type=null.

  • Initialization phase: Assign the set value to the class variable, for example, set the class variable name to "test" in the initialization phase (the symbolic reference is converted to an address reference, pointing to the constant pool).

第二步 Load Hello in the method area, find the main method, load the main method into the thread stack, and execute the main method.

third step Thread stack main analysis encountered the new keyword will perform the following operations.

Step 4 Declare the object Hello hello in the thread stack.

  • Create the execution of the object in the heap

    • 1 Load instance variables such as age into the open memory.

      • Load the instance variable age into the open memory.

      • Initializeage=0.

      • 初始化age=10. The

      • execution constructor is as follows 2

    • 2 The execution constructor is the <init> method.

第五步 Apply a memory space in the JVM heap and load Hello related information such as instance variables and instance methods from the method area into the heap memory.

六步 Point the object's declaration hello (the object reference) to the object opened in the heap memory.

参考自Q群: Java exchange discussion group (250431014) - group owner's help