Java virtual machine class loading mechanism (7)

Class loading mechanism

What is the class loading mechanism?
The Java virtual machine loads the data describing the class from the Class file into the memory, verifies the data, converts, analyzes and initializes, and finally forms a Java type that can be directly used by the virtual machine.
Each Class file represents a class or interface.

Timing of class loading

The experience of a class in virtualization is divided into seven stages: loading, verification, preparation, analysis, initialization, use, and unloading . The verification, preparation, and analysis are collectively referred to as connection. The execution sequence of the five processes of
loading, verification, preparation, initialization, and unloading is determined. Note that it may not necessarily end in sequence, and may start in one phase of the process. The first process is loading, and the virtual machine decides when to start. For initialization, it strictly stipulates that there are only six cases where the class must be initialized immediately:

  1. When encountering the four bytecode instructions new, getstatic, putstatic, and invokestatic, if the class is not initialized, it needs to start its initialization phase first. The Java scenario for generating these four instructions:
    ①Use the new keyword to instantiate an object
    ②When reading or setting a static field of a type, it is modified by final, except for the static field that has been put into the constant pool by the compiler.
    ③When calling a static method of a class
  2. When using the methods of the java.lang.reflect package to make a reflection call on a type, if the type has not been initialized, initialize it first.
  3. When the class is initialized, if the parent class is not initialized, the initialization of the parent class is triggered first.
  4. When the virtual machine starts, the user-specified main class (the class that includes the main() method) is initialized.
  5. Dynamic language support in JDK7, the final analysis result of a java.lang.invoke.MethodHandle instance is REF_getStatic, REF_putStatic,
    REF_invokeStatic, REF_newInvokeSpecial four types of method handles, and the corresponding class is not initialized, its initialization is triggered first.
  6. When an interface defines the default method in JDK8 (default modified method), if the implementation class of this interface is initialized, the interface must be initialized before it.

The behavior of these six scenarios becomes an active reference to a type, and the other reference methods will not trigger initialization, which is called passive reference.
example:

  1. Referencing the static fields of the parent class through the child class will not initialize the child class, only the parent class.
  2. The object array does not trigger the initialization of the object. People[] people = new People[10];
  3. Reference to variables in the constant pool in the class will not call the initialization of the class. Because constants are stored in the constant pool (final static) during the compilation phase.

The only difference between interface and class initialization is in the third scenario, the interface does not need to initialize the parent interface, only when it is used.

Guess you like

Origin blog.csdn.net/weixin_43663421/article/details/109273297