Use the Java class loader and class reflection examples

  • 2020-04-01 03:04:37
  • OfStack

A command corresponds to a process.

When we start a Java program, a main method, we start a Java virtual machine process, no matter how complex. Different JVM processes do not interact with each other. This is why Java programs have only one entry -- the main method, which is called by the virtual machine. The two mian methods correspond to two JVM processes, which start two different class loaders and operate on actually different classes. So it doesn't affect each other.

Class loading.

When we use a class, if the class has not been loaded into memory, the system initializes the class by loading, connecting, and initializing it.

Class loading: reading a class's class file into the JVM and creating a class object for it.

2, class connection: refers to the binary data of the class into the JRE, which is divided into three stages:

A) validation: check the correctness of the data loaded into the Class file.

B) preparation: allocate storage space to the static variables of the class and initialize them by default.

C) parsing: replace the symbolic reference in the binary data of the class with a direct reference.

3. Initialization: initialize the static variables and static initialization blocks of the class.

(note: a static property of final type, if the value of the property has been obtained at compile time, will not be initialized when the property is called, because this is equivalent to using a constant;

The ClassLoader() method is used to load the class without initializing it.

Class loaders.

The classloader is responsible for loading the.class file into memory and generating the corresponding java.lang.class object for it. It is responsible for loading all the classes, and once a class is loaded into the JVM, it will not be reloaded.

In Java, a class is identified by its fully qualified class name (package name + class name).

In the JVM, a class is identified by its fully qualified class name and its classloader.

The JVM generates three classloaders when it runs: BootstrapClassLoader(root ClassLoader), ExtClassLoader(extended ClassLoader), and AppClassLoader(system ClassLoader). The UML structure is as follows:

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201403/20140310161722.jpg? 2014210161838 ">

The BootstrapClassLoader is responsible for loading the JRE's core class library. It is not a subclass of ClassLoader and is written in C++, so we cannot see it in Java. The BootstrapClassLoader is responsible for loading the Java core class libraries such as rt.jar, charsets.jar and so on under the JRE target.

As can be seen from the figure, ExtClassLoader and AppClassLoader are subclasses of ClassLoader. They are not visible in the API, they are in the rt.jar file. Fully qualified class names are:

Sun. Misc. The Launcher $ExtClassLoader and sun. Misc. $AppClassLoader the Launcher.

The ExtClassLoader is responsible for loading the JAR package in the JRE extension directory ext, and the AppClassLoader is responsible for loading the package under the Classpath path.

The test is as follows:


package com.stopTalking.crazy;
public class TestClassLoader {
public static void main(String[] args) { 
//Gets the classloader & NBSP of the current thread;
ClassLoader loader = Thread.currentThread().getContextClassLoader();  
//Gets the class loader & NBSP; for the System class;
ClassLoader loader1 = System.class.getClassLoader();  
//Gets the classloader loader2& NBSP; of this class TestClassLoader;
ClassLoader loader2 = TestClassLoader.class.getClassLoader();  
//Gets the parent class & NBSP of loader2;
ClassLoader loader3 = loader2.getParent();  
//Gets the parent of loader2's parent class & NBSP;
ClassLoader loader4 = loader3.getParent();  
System.out.println(loader);  
System.out.println(loader1);  
System.out.println(loader2);  
System.out.println(loader3);  
System.out.println(loader4);  
}  
}

Console output:


//The class loader obtained by the current thread class is AppClassLoader
sun.misc.Launcher$AppClassLoader@6b97fd
//The System class is loaded by the root loader and not accessible in Java, so it is null
null
//The class loader for this class is of course also AppClassLoader
sun.misc.Launcher$AppClassLoader@6b97fd
sun.misc.Launcher$ExtClassLoader@1c78e57
null


Related articles: