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.
Loader class The loaded
architecture
1), Bootstrap
Classloader starts the class loader, which is mainly responsible for the coreapi# 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 javasecuritySandboxModel
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 ofstatic 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(); } } }
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!