Home > Java > javaTutorial > Detailed explanation of Java class loading mechanism ClassLoder (picture and text)

Detailed explanation of Java class loading mechanism ClassLoder (picture and text)

黄舟
Release: 2017-03-15 13:11:55
Original
2104 people have browsed it

What you see on paper is shallow, but you know you have to do it in detail --Lu You Ask the canal how clear it is so that there is a source of running water --Zhu Xi


The entire life of a class starts from being loaded into memory until it is unloaded from memory. Cycle includes: loading, verification, preparation, resolution, initialization, and using. , seven stages of unloading (Uploading). Where validation’ preparation and parsing is called linking.




1.

Class

Loader class The loaded
architecture

1), Bootstrap

Classloader starts the class loader, which is mainly responsible for the core

api# under java_home/lib ## Or the jar package specified by the -Xbootstrap option is included in the job. 2), Extension ClassLoader extension class loader, mainly responsible for the jar package under java_home/lib/ext 3), App CLassLoader system class loader, mainly responsible for Java -classpath/ The loading of classes and jar packages in the directory; 4), UserCustom ClassLoader user-defined class loader, during the running of the program, through the child of Java.

lang

.Classloader Class dynamic loading class

2. Characteristics of class loading

1) Each ClassLoader will maintain its own namespace
, two identical class names cannot appear in the same namespace

2),

In order to achieve java

securitySandboxModel

Top-level class Loader security mechanism, Java adopts the "parental delegated loading chain" structure by default.

3. There are three ways to load classes:

1). When the application is started from the command line, it is initialized and loaded by the JVM.

2), dynamic loading through the class.

forName() method.

3), dynamic loading through the ClassLoader().loadClass() method.

These three loading methods have an impact on the execution of

static code blocks.

The following example:

public class MyHello {    static    {
        System.out.println("hello word");    }
}public class Hello {    public static void main(String[] args) {
        ClassLoader loader=Hello.class.getClassLoader();        
        try {            
        //静态代码块不运行            
        //loader.loadClass("Test.MyHello");            
        //静态代码块运行输出hello world            
        //Class.forName("Test.MyHello");            
        //静态代码块不运行            
        //Class.forName("Test.MyHello", false, loader);            
        //静态代码块运行输出hello world            
        Class.forName("Test.MyHello", true, loader);        
        } 
        catch (ClassNotFoundException e) {            
        // TODO 自动生成的 catch 块            
        e.printStackTrace();       
         }
    }
}
Copy after login

1. ClassLoader class loading architecture

1) Bootstrap Classloader starts the class loader, which is mainly responsible for loading the core api under java_home/lib or the jar package specified by the -Xbootstrap option.

2), Extension ClassLoader extension class loader, mainly responsible for the jar package under java_home/lib/ext

3), App CLassLoader system class loader, mainly responsible for Java -classpath/ The loading of classes and jar packages in the directory;

4), UserCustom ClassLoader user-defined class loader, during the running of the program, dynamically load classes through the subclasses of Java.lang.Classloader

The above is the detailed content of Detailed explanation of Java class loading mechanism ClassLoder (picture and text). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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