Home >Java >javaTutorial >Java class loader and class reflection usage examples

Java class loader and class reflection usage examples

高洛峰
高洛峰Original
2017-01-13 09:22:331529browse

1. One command corresponds to one process.

When we start a Java program, that is, start a main method, a Java virtual machine process will be started, no matter how complex the process is. Different JVM processes will not affect each other. This is why it is said that the Java program has only one entrance-the main method, which is called by the virtual machine. The two main methods correspond to two JVM processes, starting two different class loaders, and actually operating different classes. Therefore they will not affect each other.

2. Class loading.

When we use a class, if the class has not been loaded into memory, the system will initialize the class through loading, connection, and initialization.

1. Class loading: refers to reading the class file of the class into the JVM and creating a Class object for it.

2. Class connection: refers to merging the binary data of the class into the JRE, which is divided into 3 stages:

a), verification: check the loaded Class file data correctness.

b). Preparation: Allocate storage space to the static variables of the class and perform default initialization.

c), parsing: replace the symbol references in the binary data of the class with direct references.

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

(Note: For a final type static property, if the property value has been obtained at compile time, then calling the property will not cause the class to be initialized, because this is equivalent to using a constant;

Use the ClassLoader() method, which only loads the class and does not initialize it.)

3. Class loader.

The class loader is responsible for loading .class files into memory and generating corresponding java.lang.Class objects. It is responsible for loading all classes. Once a class is loaded into the JVM, It will not be loaded again.

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

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

JVM will generate 3 ClassLoaders when running, namely: BootstrapClassLoader (root class loader), ExtClassLoader (extended class loader) and AppClassLoader (system class loader). The UML structure is as follows:

Java class loader and class reflection usage examples

## Among them, BootstrapClassLoader is responsible for loading the core class library of JRE. It is not a subclass of ClassLoader and is written in C++, so we are It is not visible in Java and will return null when obtained through the getParent() method of its subclass. BootstrapClassLoader is responsible for loading Java core class libraries such as rt.jar and charsets.jar under the JRE target.

As can be seen from the figure, ExtClassLoader and AppClassLoader are subclasses of ClassLoader. You can't see them in the API, they are in the rt.jar file. The fully qualified class names are:

sun.misc.Launcher$ExtClassLoader and sun.misc.Launcher$AppClassLoader.

Among them, ExtClassLoader is responsible for loading the JAR package in the JRE extension directory ext, and AppClassLoader Responsible for loading class packages under the Classpath path.

The test is as follows:

package com.stopTalking.crazy;
public class TestClassLoader {
public static void main(String[] args) { 
//获取当前线程的类装载器  
ClassLoader loader = Thread.currentThread().getContextClassLoader();  
//获取System类的类装载器  
ClassLoader loader1 = System.class.getClassLoader();  
//获取本类TestClassLoader的类装载器loader2  
ClassLoader loader2 = TestClassLoader.class.getClassLoader();  
//获取loader2的父类  
ClassLoader loader3 = loader2.getParent();  
//获取loader2的父类的父类  
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:

//当前线程类获取的类加载器是AppClassLoader
sun.misc.Launcher$AppClassLoader@6b97fd
//System类为根装载器加载,java中访问不到,所以为null
null
//本类的类加载器当然也是AppClassLoader
sun.misc.Launcher$AppClassLoader@6b97fd
sun.misc.Launcher$ExtClassLoader@1c78e57
null

For more java class loader and class reflection usage examples related articles, please pay attention to the PHP Chinese website!

Statement:
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