Java Class load 2

Source: Internet
Author: User

1. 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 load the binary data of the class;

2) Links:

Validation: Ensure that the class being loaded is correct;

Prepare: Allocates memory for static variables of the class and initializes them to default values;

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.

2. Initialization of classes

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, then 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.

3. Loading of classes

Class loading refers to reading the binary data in the class's. class file into memory, placing it in the method area of the run-time data area, and then creating a Java.lang.Class object of this class in the heap that encapsulates the class's objects in the method area class. Look at the following 2 figures

The end product loaded by the class is a class object that is located in the heap area
The class object encapsulates the data structure of the classes within the method area, and provides the Java programmer with an interface to access the data structures within the method area

There are several ways to load classes:

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,jar

4) Extract the. class file from the proprietary database

5) Dynamically compile Java source files into a. class file (server)

4. Loader

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

The class loading of the JVM is done through ClassLoader and its subclasses, and the hierarchy and loading order of the classes can be described as follows:

1) Bootstrap ClassLoader

Responsible for loading all classes in Jre/lib/rt.jar in $java_home, implemented by C + +, not classloader subclasses

2) Extension ClassLoader

Some jar packages that are responsible for loading the extensions in the JAVA platform, including the jar packages in $java_home Jre/lib/*.jar or-djava.ext.dirs specified directories

3) App ClassLoader

Responsible for documenting the jar packages specified in the Classpath and the class in the directory

4) Custom ClassLoader

The ClassLoader that the application customizes according to its own needs, such as Tomcat and JBoss, will be implemented according to the Java EE specification itself ClassLoader

The load process checks to see if the class is loaded, the check order is bottom-up, and the custom ClassLoader to bootstrap ClassLoader-by-layer check, as long as a certain classloader is loaded as if the class has been loaded. Ensure that only all classloader of this class are loaded once. The order of loading is top-down, that is, the upper layer tries to load the class one at a level.

Java Class load 2

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.