Annotation and reflection 04--class loading

Java memory analysis

insert image description here

Understand the class loading process

When a program actively uses a certain class, if the class has not been loaded into the memory, the system will initialize the class through the following three steps
insert image description here

Class loading and understanding of ClassLoader

1. Loading: Load the bytecode content of the class file into the memory, convert these static data into the runtime data structure of the method area, and then generate a java.lang.Class object representing this class.
2. Linking: the process of merging the Java binary code into the running state of the JVM.
(1) Verification: Ensure that the loaded class information complies with the JVM specification and there are no security issues.
(2) Preparation: The stage of formally allocating memory for the class variable (static) and setting the default initial value of the class variable. These memory will be allocated in the method area.
(3) Analysis: The symbolic reference (constant name) in the virtual machine constant pool is transformed into a direct reference (address).
3. Initialization:
(1) The process of executing the class constructor () method. The class constructor () method is generated by merging the copying action of all class variables in the automatic mobile class at compile time and the rain gear in the static code. (The class constructor constructs class information, not the constructor that constructs objects of that class).
(2) When initializing a class, if you find that its parent class has not been initialized, you need to start the initialization of its parent class first.
(3) The virtual machine ensures that the () method of a class is correctly locked and synchronized in a multi-threaded environment.

public class Test02 {
    
    
    public static void main(String[] args) {
    
    
        A a = new A();
        System.out.println(A.m);
    }
}

class A{
    
    
    static {
    
    
        System.out.println("A类静态代码初始化");
        m = 300;
    }

    static int m = 100;

    public A(){
    
    
        System.out.println("A类的无参构造初始化");
    }
}

After running, it can be found that the value of A is 100. From the output results, we can see that the static code of class A is initialized first, so m is equal to 300 first and then equal to 100.
insert image description here

When does initialization of a class occur

1. Active reference of the class (the initialization of the class will definitely occur)
(1) When the virtual machine starts, first initialize the class where the main method is located
(2) new an object of a class
(3) call the static member of the class (except the final constant) and static methods
(4) Use the methods of the java.lang.reflect package to make reflective calls to classes
(5) When initializing a class, if its parent class has not been initialized, its parent class will be initialized first
2. Passive reference of the class (Class initialization will not occur)
(1) When accessing a static field, only the class that actually declares this field will be initialized. For example: when the static variable of the parent class is referenced through the subclass, it will not be initialized by the subclass.
(2) The reference of the class definition through the array will not trigger the initialization of this class
(3) The reference constant will not trigger the initialization of this class (the constant is stored in the constant pool of the calling class during the linking phase)

public class Test03 {
    
    

    static {
    
    
        System.out.println("Main类被加载");
    }

    public static void main(String[] args) throws ClassNotFoundException {
    
    
        //主动引用
        Son son = new Son();

        //不会产生类的引用方法
        System.out.println(Son.b);
    }
}

class Father{
    
    
    static int b = 2;

    static {
    
    
        System.out.println("父类被加载");
    }
}

class Son extends Father{
    
    
    static {
    
    
        System.out.println("子类被加载");
    }
}

class loader

The role of class loaders

The role of the class loader: load the bytecode content of the class file into the memory, and convert these static data into the runtime data section of the method area, and then generate a java.lang representing this class in the heap. Class object, as the access entry of class data in the method area.
Class cache: The standard JavaSE class loader can look up classes as required, but once a class is loaded into the class loader, it will maintain a load cache for a period of time. However, the JVM garbage collection mechanism can reclaim these Class objects.
insert image description here
The role of the class loader is to load the class (class) into memory. The JVM specification defines the following types of class loaders:
(1) Boot class loader: written in C++, it is the class loader that comes with the JVM, responsible for the core library of the java platform, and used to load the core class library. This loader cannot be obtained directly.
(2) Extended class loader: responsible for loading jar reports in the jre/lib/ext directory or jar reports in the directory specified by -D java.class.path into the working library (3) Subclass loader
: responsible for java-classpath or -D java.class.path refers to the class and jar package into the directory, which is the most commonly used loader.

Guess you like

Origin blog.csdn.net/cang_ling/article/details/131977230