Class loading process

When we execute the main method of a class using a Java command, the main class is first loaded into the JVM through the classloader

package org.laugen.jvm;

public class Math {
    public static int intData = Awesome!;
    public static User user = new User();

    public int compute(a) {
        int a = 1;
        int b = 2;
        int c = (a + b) * 10;
        return c;
    }

    public static void main(String[] args) {
        Math math = newMath(); math.compute(); }}Copy the code

The general flow for loading the Math class above is as follows:

  • C++ creates a bootstrap class loader (BootstrapClassLoader) the instance of the
  • C++ calls Java code to create instances of JVM initiatorssun.misc.LauncherThe class is loaded by the boot class loader and other class loaders are created
  • Launcher.getLauncher()->launcher.getClassLoader()To get the Math classloaderAppClassLoaderThe instance
  • classLoader.loadClass("org.laugen.jvm.Math")To call the class loaderloadClassMethod loading class
  • After the class is loaded, C++ calls math.main ()

The loadClass process has the following steps:

Load >> Verify >> Prepare >> Parse >> Initialize >> Use >> Uninstall

  • Load: The bytecode file is read and loaded only when the Class is used, such as calling the static method of the Class, instantiating the object of the Class, etc. During the loading phase, a Class object is generated and stored in memory, which is used as the access point for the data of the Class in the method area
  • Validation: Verifies the correctness of the bytecode file
  • Preparation: Allocates memory for static variables of the class and assigns default values
  • Resolution: the replacement symbol references for direct reference to the stage will put some static methods (symbol references, such as the main () method) to replace for the pointer to the data storage, memory, or a handle, etc. (reference) directly, this is the so-called static linking process (complete) during class loading, dynamic linking is done during the program is running will use replacement for direct reference symbols
  • Initialization: Executes a static block of code to initialize a class’s static variable to the specified value

After a class is loaded into the method area, it mainly contains the runtime constant pool, type information, field information, method information, class loader reference, reference to the corresponding class instance and so on

  • Class loader reference: A reference from this class to an instance of the class loader
  • Reference to a corresponding class instance: After the class information is loaded into the method area, the class loader creates an object instance of the corresponding class type and places it in the Heap, which acts as an entry point and pointcut for developers to access the class definition in the method area.

Note: If the main class uses other classes during runtime, these classes are progressively loaded. Classes in a JAR or WAR are not loaded all at once; they are loaded when used.

Here is a piece of code. Can you tell the result?

package org.laugen.jvm;

public class DynamicLoad {

    static {
        System.out.println("load DynamicLoad");
    }

    public static void main(String[] args) {
        new A();
        // B will not load
        B b = null;
        // C will load
        C c = new C();
        // D will loadD.staticD(); }}class A {
    static {
        System.out.println("load A");
    }

    public A(a) {
        System.out.println("initial A"); }}class B {
    static {
        System.out.println("load B");
    }

    public B(a) {
        System.out.println("initial B"); }}class C {
    static {
        System.out.println("load C");
    }

    public C(a) {
        System.out.println("initial C"); }}class D {
    static {
        System.out.println("load D");
    }

    public D(a) {
        System.out.println("initial D");
    }

    public static void staticD(a) {
        System.out.println("static D"); }}Copy the code