Java ClassLoader Detailed

Source: Internet
Author: User

1. What is a classloader?

The ClassLoader is looking for a bytecode file for a class and constructs an object component that the class represents within the JVM.

The class is classloader and its subclasses are responsible, and ClassLoader is an important Java Runtime system component that is responsible for locating and loading class bytecode files at runtime.

JVM runtime generates three classloader:

BootStrap ClassLoader: Written by C + +, non-ClassLoader subclasses, not seen in Java, the root loader is mainly responsible for the JRE core class library, under the Rt.jar, Resources.jar, Charset.jar.

Ext ClassLoader: The extension classloader, which is responsible for loading the jar class package in the JRE extension directory ext, is mainly used for third-party extension functions.

APP ClassLoader: Responsible for loading the class package under Classpath.

Three class loaders the relationship between the two can be described by the image:

2. Loading process for classes

The JVM divides the class loading process into three steps: Mount (load), link, and Initialize (Initialize) links are divided into three steps, as shown in:


1) Load: Find and import class files;

2) Link: Perform validation, preparation, and resolution steps

A) Validation: Ensure that class files are loaded with the correct classes;

b) Prepare: Allocate memory for static variables of the class and initialize them to default values;

c) Parse: Converts a symbolic reference in a class to a direct reference;

3) Initialization: Assigns the correct initial value to the static variable of the class;

Then why do I have to verify this step? First of all, if the class file generated by the compiler, it certainly conforms to the JVM bytecode format, but in case there is a master to write a class file, let the JVM load and run for malicious purposes, it is not good, so this class file to verify this first pass, If you don't, you won't let it go on, it's for security reasons.

The preparation phase and the initialization phase seem a bit Shang, in fact, is not Shang, if the class has a statement: private static int a = 10, it is the execution of the process is such, first the bytecode file is loaded into memory, the first link verification this step, verify through the post-preparation phase, to a allocated memory, Because the variable A is static, at this point A is equal to the default initial value of type int 0, that is, a=0, and then to the resolution (said later), to the initialization of this step, the true value of a is 10 assigned to a, at this time a=10.

3. Initialization of classes

Note: from http://blog.csdn.net/gjanyanlig/article/details/6818655

When the class is initialized:

1) Create an instance of the class, that is, the new object

2) Access a static variable of a class or interface, or assign a value to the static variable

3) Calling the static method of the class

4) Reflection (Class.forName ("Com.lyj.load"))

5) Initializes a subclass of a class (the parent class of the child class is initialized first)

6) Startup class indicated at the start of the JVM, which is the same class with the same file name and class name

Only these 6 cases will cause the class's class to be initialized.

Initialization steps for the class:

1) if this class has not been loaded and linked, load and link it first

2) If the class has a direct parent class, and the class has not been initialized (note: In a classloader, the class can only be initialized once), then initialize the immediate parent class (not for the interface)

3) If there is an initialization statement (such as a static variable and a static block) in the join class, then the initialization statements are executed sequentially.

4. Parental delegation mechanism

is the ClassLoader load class flowchart to load a class of procedure class examples that illustrate the entire classloader process.


The loading of ClassLoader in Java takes the "parental delegation mechanism (overall responsibility for delegation mechanism)".

Parental delegation mechanism: when a particular class loader receives a request to load a class, it first delegates the load task to the parent class loader, recursively, and returns if the parent ClassLoader can complete the class load task, only to load itself when the parent ClassLoader cannot complete the load task.

< Span style= "Color:rgb (51,51,51); line-height:23px; letter-spacing:0.5px; font-family: ' Microsoft yahei ', Verdana,sans-serif, Song body ">    Benefits: The Java class has a hierarchical relationship with precedence as its classloader. For example, the class Java.lang.Object, which is stored in Rt.jar, regardless of which class loader is loading the class, will eventually be delegated to the startup ClassLoader for loading, so the Object class is the same class in the various class loader environments of the program. Conversely, if the user writes a class named Java.lang.Object and puts it in the program's classpath, there will be several different object classes in the system, and the most basic behavior in the Java type System is not guaranteed and the application becomes chaotic. The parent delegation mechanism guarantees that the class is always loaded from the root loader.

Java development, have encountered java.lang.NoSuchMethodError errors, the reason is that the JVM's "parental delegation mechanism" caused by the Java class path to the different versions of the class package, such as Both Commons-lang2.x.jar and Commons-lang3.x.jar are in the classpath, and the code uses a method of the Common-lang3.x.jar class, which does not exist in Commons-lang2.x.jar, and the JVM is loaded by chance from COM Mons-lang2.x.jar The class is loaded, the runtime throws a nosuchmethoderror error.








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java ClassLoader Detailed

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.