Java class loading process study notes

Java class loading process

The JVM divides the class loading process into three steps: loading, linking, and initialization, where linking is divided into three steps: verification, preparation, and parsing.

class loading process

  • Load load: find and load the binary data of the class;

  • link link:

    1. Verify verify: ensure the correctness of the loaded class;
    2. Prepare preparation: allocate memory for static variables of the class and initialize them to default values;
    3. Parse resolution: convert symbolic references to classes to direct references
  • Initialization initialization: assign correct initial values ​​to the static variables of the class;

class initialization

6 situations can lead to the initialization of a class:

public class LoadMethod {

    static {
        System.out.println("LoadMethod is Load");
    }

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {

        // 1.第一次实例化类
        ClassA classA = new ClassA();

        // 5.反射
        Class<ClassA> classAClass = (Class<ClassA>) Class.forName("com.stupidzhe.jdklearning.classloader.ClassA");

        // 3.调用类中的静态变量
        System.out.println(ClassA.name);

        // 4.调用类中的静态方法
        ClassA.print();

        // 5.初始一个类的子类
        ClassB classB = new ClassB();

        // 6.该类被作为启动类
    }
}

Class initialization steps

1) If the class has not been loaded and linked, then load and link first;

2) If the class has a direct parent class, and the parent class is not initialized, then initialize the direct parent class (ps. In a class loader, the class can only be loaded once);

3) If there are initialization statements in the class, execute those initialization statements.

class loading

Class loading refers to reading the binary data in the .class file of the class into memory, placing it in the method area of ​​the runtime data area, and then creating a Class object of this class in the heap area to encapsulate the class An object of the class in the method area.

The Class object encapsulates the data structure (such as static variables and constants) of the class in the method area, and provides the programmer with an interface to access the data structure in the method area; there are several ways to load a class:

  1. Load directly from local system
  2. Download the .class file from the network
  3. Load .class file from archive
  4. Compile Java source files dynamically into .class files
  5. Extract .class files from proprietary database

Loader

JVM class loading is done through ClassLoader and its subclasses.

  1. Bootstrap ClassLoader: responsible for loading jre/lib/rt.jar in $JAVA_HOME, which is implemented by cpp;
  2. Extension ClassLoader: Responsible for loading some jar packages of Java platform extension functions, including jar packages in the directory specified by jre/lib/ext/*.jar or -Djava.ext.dirs in $JAVA_HOME;
  3. App ClassLoader: Responsible for loading the jar package specified in the classpath and the class in the directory;
  4. Custom ClassLoader: It is a user-defined ClassLoader, which is implemented according to the j2ee specification.

JVM class loading through parent delegation mode

Why use the parental delegation model?

The advantage is that a Java class has a prioritized hierarchy along with its loader, which avoids repeated loading of classes .

ps. There is also a context class loader that does not conform to the parent delegation model

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325018189&siteId=291194637