Java namespace naming Rules Page 1/2
Summary
Java classloaders are the core of Java's dynamic nature. This article will briefly introduce Java classloaders, related parent delegation models, namespaces, runtime packages, and other concepts, at the same time, we discuss some problems that are easy to confuse in learning.
Functions and categories of class loaders
As the name suggests, the class loader is used to load classes into JVM. The JVM Specification defines two types of class loaders: start class loader (bootstrap) and user-defined loader (user-defined class loader ).
Bootstrap is a class loader that comes with JVM and is used to load core class libraries, such as java. lang. For example, java. lang. Object is loaded by bootstrap.
Java provides the abstract class ClassLoader. All user-defined class loaders instantiate the class of the class loader. System Class Loader is a special user-defined Class Loader provided by the JVM implementer. It is loaded by default when the programmer does not specify a Loader. The system class loader can be obtained through the ClassLoader. getSystemClassLoader () method.
Example 1: Test the ClassLoader of the JVM
/* LoaderSample1.java */public class LoaderSample1 {public static void main (String [] args) {Class c; ClassLoader cl;
Cl = ClassLoader. getSystemClassLoader (); System. out. println (cl );
While (cl! = Null) {cl = cl. getParent (); System. out. println (cl);} try {c = Class. forName ("java. lang. object "); cl = c. getClassLoader (); System. out. println ("java. lang. object's loader is "+ cl); c = Class. forName ("LoaderSample1"); cl = c. getClassLoader (); System. out. println ("LoaderSample1's loader is" + cl);} catch (Exception e) {e. printStackTrace ();}}}
Running results on my machine (Sun Java 1.5)
C: \ java> java LoaderSample1
Sun. misc. Launcher $ AppClassLoader @ 82ba41
Sun. misc. Launcher $ ExtClassLoader @ 923e30
Null
Java. lang. Object's loader is null
LoaderSample1's loader is sun. misc. Launcher $ AppClassLoader @ 82ba41
The first line indicates that the system class loader is instantiated from the sun. misc. Launcher $ AppClassLoader class.
The second line indicates that the parent of the system class loader is instantiated from the sun. misc. Launcher $ ExtClassLoader class.
The third line indicates that the parent of the system class loader is bootstrap.
The fourth line indicates that the core class java. lang. Object is loaded by bootstrap.
The fifth line indicates that the user class LoaderSample1 is loaded by the system class loader.
Parent delegation Model
Beginning with version 1.2, Java introduced the parent-parent Delegation Model to better ensure the security of the Java platform. In this model, when a loader is requested to load a Class, it first delegates its own parent for loading. If the parent can be loaded, it returns the Class object corresponding to the Class, if the parent cannot be loaded, it is loaded by the parent requestor.
As shown in 1, the parent of loader2 is loader1, and the parent of loader1 is system class loader. Assume that loader2 is required to load the class MyClass. In the parent delegation Model, loader2 first requests loader1 for loading, and loader1 requests the system class loader for loading MyClass. If the system loader can be loaded successfully, the reference of the Class Object corresponding to MyClass is returned to loader1, and loader1 returns the reference to loader2, so that the Class MyClass is successfully loaded into the virtual machine. If the system class loader cannot load MyClass, loader1 will try to load MyClass. If loader1 cannot load successfully, loader2 will try to load. If neither the parent nor loader2 can be loaded, the loading fails.
If one of the Class loaders can be loaded successfully, the actual Class loaders are called the definition Class loaders, and all the loaders (including the definition Class loaders) that can successfully return Class objects) it is called an initial class loader. 1. Assume that loader1 actually loads MyClass, loader1 is the definition class loader of MyClass, and loader2 and loader1 are the initial class loaders of MyClass.
Current 1/2 page12. Next page