Java class loader and class reflection use example _java

Source: Internet
Author: User
Tags reflection thread class

The de jure command corresponds to a process.

When we start a Java program that starts a main method, it starts a Java virtual machine process, no matter how complex the process is. Different JVM processes do not interact with each other. This is why the Java program has only one entry--main method that allows the virtual machine to invoke. The two Mian methods, which correspond to 2 JVM processes, start with two different Class loaders, which actually operate in different classes. So they don't affect each other.

Second, class loading.

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

1, class loading: Refers to the class file read into the JVM, and create a class object for it.

2, class Connection: Refers to the binary data of the class merged into the JRE, which is divided into 3 stages:

A), checksum: Check the correctness of loading class file data.

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

c), parsing: Replaces a symbolic reference in a class's binary data with a direct reference.

3, initialization: The class of static variables, static initialization block initialization.

(Note: A final type of static property that, if the property value has been obtained at compile time, does not cause the class to be initialized when it is invoked, as this is equivalent to using a constant;

Using the ClassLoader () method, only the class is loaded and is not initialized. )

Third, class loader.

The ClassLoader is responsible for loading the. class file into memory and generating the corresponding Java.lang.Class object, which is responsible for loading all classes, and once a class is loaded into the JVM, it is not loaded again.

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

In the JVM, a class is identified with its fully qualified class name and its ClassLoader.

The JVM runs with 3 ClassLoader: Bootstrapclassloader (Root class loader), Extclassloader (extended class loader), and Appclassloader (System class loader). The UML structure is as follows:

Where Bootstrapclassloader is responsible for loading the JRE's core class library, which is not a subclass of ClassLoader, is written in C + +, so we don't see it in Java, and returns null when fetched by the GetParent () method of its subclass. Bootstrapclassloader is responsible for loading Rt.jar, Charsets.jar, and other Java core class libraries under the JRE target.

As the figure shows, Extclassloader and Appclassloader are subclasses of ClassLoader. They are not visible in the API, they are located in the Rt.jar file. The fully qualified class names are:

Sun.misc.launcher$extclassloader and Sun.misc.launcher$appclassloader.

Where Extclassloader is responsible for loading the jar package in the JRE extension directory ext, and Appclassloader is responsible for loading the class pack under the Classpath path.

The test is as follows:

Copy Code code as follows:

Package com.stopTalking.crazy;
public class Testclassloader {
public static void Main (string[] args) {
Gets the class loader for the current thread
ClassLoader loader = Thread.CurrentThread (). Getcontextclassloader ();
Gets the class loader for the system class
ClassLoader loader1 = System.class.getClassLoader ();
Gets the class loader for this class Testclassloader Loader2
ClassLoader loader2 = TestClassLoader.class.getClassLoader ();
Get Loader2 's parent class
ClassLoader Loader3 = Loader2.getparent ();
Gets the parent class of the Loader2 parent class
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:

Copy Code code as follows:

The class loader obtained by the current thread class is Appclassloader
Sun.misc.launcher$appclassloader@6b97fd
The system class is loaded for the root loader and cannot be accessed in Java, so null
Null
Class loaders of this class are of course also appclassloader
Sun.misc.launcher$appclassloader@6b97fd
Sun.misc.launcher$extclassloader@1c78e57
Null

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.