Java class loader summary, java loading Summary

Source: Internet
Author: User

Java class loader summary, java loading Summary

1. class loading process

The JVM divides the class loading process into three steps: Load, Link, and Initialize links, as shown in:

1) load: Find and load binary data of the class;

2) link:

Verify: ensure the correctness of the loaded class;

Preparation: allocate memory for static variables of the class and initialize it as the default value;

Resolution: converts a symbolic reference in a class to a direct reference;

3) initialization: Assign the correct initial value to the static variables of the class;

So why do I need to verify this step? First, if the class file generated by the compiler is in line with the JVM byte format, but if a master writes a class file by himself, let the JVM load and run it for malicious purposes, this is not good. Therefore, this class file must be verified first. If it does not meet the requirements, it will not be executed again. It is also for security consideration.

The preparation and initialization phases seem to be a bit shield, but they are not shield. If there is a statement in the class: private static int a = 10, the execution process is like this, first, after the bytecode file is loaded to the memory, perform the link verification step first. After the verification is passed, the preparation phase will allocate memory to a because the variable a is static, so at this time, a is equal to the default value of int Type 0, that is, a = 0, and then to the parsing (as described later), to the initialization of this step, to assign the true value of a to a. At this time, a = 10.

 

2. class initialization

Class initialization:

1) create a class instance, that is, a new object

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

3) Call static methods of Classes

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

5) initialize a subclass of a class (the parent class of the subclass will be initialized first)

6) The JVM startup class, that is, the class with the same file name and class name.

Only these 6 conditions will lead to class initialization.

Class initialization steps:

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

2) If this class has a direct parent class and the class has not been initialized (Note: In a class loader, the class can only be initialized once ), initialize the direct parent class (not applicable to interfaces)

3) if there are initialization statements (such as static variables and static blocks) added to the class, execute these initialization statements in sequence.

 

3. Loading of Classes

Class loading refers to the operation of the class. the binary data in the class file is read into the memory, stored in the Method Area of the runtime data area, and a Java of this class is created in the heap area. lang. class Object, used to encapsulate objects of a Class in the method area. See figure 2 below

The final product of Class loading is the Class object located in the heap area.
The Class Object encapsulates the data structure of the Class in the Method Area, and provides the Java programmer with an interface to access the data structure in the method area.

You can load classes in the following ways:

1) load directly from the local system

2) download the. class file over the network

3) load. class files from archive files such as zip files and jar files

4) extract the. class file from apsaradb

5) dynamically compile the Java source file into a. class file (server)

 

4. Loader

From http://blog.csdn.net/cutesource/article/details/5904501

 

JVM class loading is completed by ClassLoader and its sub-classes. The class hierarchy and loading sequence can be described from the following:

1) Bootstrap ClassLoader

Loads all the classes in jre/lib/rt. jar in $ JAVA_HOME, implemented by C ++, not the ClassLoader subclass.

2) Extension ClassLoader

Loads some jar packages for the extended functions of the java platform, including the jar packages under the specified directory of jre/lib/*. jar or-Djava. ext. dirs in $ JAVA_HOME.

3) App ClassLoader

Records the specified jar package in classpath and the class in the directory.

4) Custom ClassLoader

ClassLoader defined by applications according to their own needs. For example, tomcat and jboss implement ClassLoader based on j2ee specifications.

During the loading process, the system first checks whether the class has been loaded. The check sequence is from bottom to top. From Custom ClassLoader to BootStrap ClassLoader, this class is regarded as loaded as long as a classloader has been loaded, make sure that this class is only loaded once by all ClassLoader. The order of loading is from top to bottom, that is, the upper layer tries to load this type.

Notes for learning Java !!!

If you have any questions or want to obtain learning resources during the learning process, join the Java learning exchange group with the group number 618528494.Let's learn Java together!

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.