Home  >  Article  >  Java  >  Analysis of process examples of java class loading

Analysis of process examples of java class loading

WBOY
WBOYforward
2023-05-14 15:25:061384browse

Explanation

When a program actively uses a class, if the class has not been loaded into memory, the system will initialize the class through the following three steps.

Class loading steps

1. Loading: Load the bytecode content of the class file into memory, and convert these static data into runtime data in the method area structure, and then generate a java.lang.Class object representing this class, which serves as the access entrance to the class data in the method area.

2. Linking: The process of merging the binary code of the Java class into the running state of the JVM.

3. The process of executing the class constructor () method.

Example

public class ClassLoadingTest{
    public static void main (String [] args){
        System.out.println(test.m);
    }
}
 
class test{
    static {
        m = 300;
    }
    static int m = 100;
}
//第一步:加载
//第二步:链接结束后m=0
//第三步:初始化结束后,m的值由<clinit>()方法执行决定
/*
这个test构造器<clinit>()方法由类变量的赋值和静态代码块中的语句按照顺序合并产生,类似于
<clinit>(){
m = 300;
m = 100;
}
*/

The above is the detailed content of Analysis of process examples of java class loading. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete