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

Analysis of process examples of java class loading

WBOY
Release: 2023-05-14 15:25:06
forward
1465 people have browsed it

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;
}
*/
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template