Java class Loader

Source: Internet
Author: User

First, classification:

1.BootStrap class Loader: Starts the ClassLoader and is responsible for loading all Java classes in the Rt.jar file, that is, the core class of Java is loaded by the ClassLoader. In Sun jdk, this classloader is implemented by C + + and is not available for reference in the Java language.

2.Extension class Loader: Extension ClassLoader, which is responsible for loading some of the extended functionality of the JAR package.

3.System class Loader: The System class loader, which is responsible for loading the jar packages and directories in the classpath specified in the startup parameters, usually the Java classes we write ourselves are also loaded by the ClassLoader. In the Sun JDK, the system ClassLoader is named Appclassloader.

4.User Defined class Loader: User-defined class loaders, which are loaded by user-defined classes, can manually control the steps in the loading process.

Second, the working principle:

Class loading is divided into three steps: Load, link, and initialize.

1. Loading

The class is loaded with the fully qualified name and ClassLoader of the class, primarily by loading the specified. class file into the JVM. When the class is loaded, the class is marked with the fully qualified name +classloader instance ID of the class within the JVM.

In memory, instances of ClassLoader instances and classes are located in the heap, and their class information is in the method area.

The loading process employs a method known as the parent delegation model, and when a classloader loads a class, it asks its parent classloader (in fact there are only two ClassLoader, So called the parent ClassLoader may be easier to understand) the load class, and its parent ClassLoader will continue to commit the load request to a higher level of classloader until the class loader is started. Only the parent ClassLoader cannot load the specified class, it will load the class itself.

The parent delegation model is the JVM's first security line of defense, which guarantees the safe loading of classes, which also relies on the principle of class loader isolation: There is no direct interaction between classes loaded by the same loader, and even if the same class is loaded by different classloader, they cannot perceive each other's existence. Thus, even if a malicious class impersonates itself under a core package (for example, Java.lang), it cannot be compromised because it cannot be loaded by the startup ClassLoader.

It can also be seen that if the user customizes the ClassLoader, it is necessary to secure the class loading process.

2. Links

The task of linking is to merge the binary type information into the JVM runtime state.

The link is divided into the following three steps:

A. Validation: Verify the correctness of the. class file, ensure that it is compliant with the specification, and is suitable for use by the current JVM.

B. Prepare: Allocates memory for the class, while static variables in the initialization class are assigned the default values.

C. Parsing (optional): The main point is to resolve the symbolic reference in the class's constant pool to a direct reference, which can be resolved when the appropriate reference is used.

3. Initialization

Initializes a static variable in the class and executes the static code, constructor, in the class.

The JVM specification strictly defines when a class needs to be initialized:

A, when instantiating an object by using the New keyword, reflection, clone, and deserialization mechanism.

B, when invoking a static method of a class.

C, when you use a static field of a class or assign a value to it.

D. When invoking a method of a class by reflection.

E, when initializing subclasses of the class (the parent class must have been initialized before the subclass is initialized).

F, the class that is marked as the startup class when the JVM is started (simply understood as a class with the Main method).

Java class Loader

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.