1. Class loading Process
The JVM divides the class loading process into three steps: Mount (load), link and initialization (Initialize) links are divided into three steps, as shown in the following figure:
1) Loading: Find and load the binary data of the class;
2) Link:
Validation: Ensure the correctness of the loaded class;
Preparation: Allocates memory for the class's static variable and initializes it to the default value;
Parsing: Converts a symbolic reference in a class to a direct reference;
3 initialization: Give the correct initial value to the static variable of the class;
So why do I have to verify this step. First of all, if the compiler generated the class file, it must be in the JVM bytecode format, but in case there is a master to write a class file, let the JVM load and run, for malicious use, it is not good, so this class file to be validated before the pass, If it doesn't, it won't let it go on, it's for security reasons.
The preparation phase and initialization phase seem a bit Shang, actually is not Shang, if there is a statement in the class: private static int a = 10, its execution is this, first byte code file is loaded into memory, the first step to verify the link, verification through the preparation phase, to a allocated memory, Because variable A is static, at this point A is equal to the default initial value of type 0, that is, a=0, and then to parse (after that), to initialize this step, the true value 10 of a is assigned to a, at this time a=10.
2. Initialization of Classes
When will the class be initialized:
1 Create an instance of the class, which is the new object
2 Accessing a static variable of a class or interface, or assigning a value to the static variable
3 static method of calling class
4) Reflection (Class.forName ("Com.lyj.load"))
5 Initializes a subclass of a class (the parent class of the subclass is initialized first)
6 The Startup class marked at startup of the JVM, that is, the class with the same filename and class name
Only these 6 situations can cause class initialization of classes.
Initialization steps for a class:
1 if this class has not yet been loaded and linked, then load and link 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 initialize once), then initialize the direct parent class (not applicable to the interface)
3 If there are initialization statements (such as static variables and static blocks) in the join class, the initialization statements are executed sequentially.
3. Class loading
Class loading refers to reading binary data in the. class file of the class 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 area to encapsulate the object of the class in the method area class. Look at the 2 pictures below.
The final product of a class is the class object that is in the heap area
class objects encapsulate the data structure of classes within the method area and provide Java programmers with an interface to access data structures within the method area
There are several ways to load classes:
1 direct loading from local system
2 Download the. class file over the network
3) Load the. class file from the archive file such as Zip,jar
4 Extract the. class file from the proprietary database
5 Dynamically compile Java source files into. class file (server)
4. Loader
from http://blog.csdn.net/cutesource/article/details/5904501
Class loading of the JVM is done through ClassLoader and its subclasses, and the hierarchy of classes and loading order can be described in the following illustration:
1) Bootstrap ClassLoader
Responsible for loading all class in Jre/lib/rt.jar in $java_home, implemented by C + +, not ClassLoader subclass
2) Extension ClassLoader
Some jar packages that are responsible for loading extensions in the JAVA platform, including the jar packages in Jre/lib/*.jar or-djava.ext.dirs specified directories in $java_home
3) App ClassLoader
Responsible for documenting the jar package and class in the directory specified in Classpath
4) Custom ClassLoader
ClassLoader that belong to the application to customize according to their own needs, such as Tomcat, JBoss will be implemented according to the Java EE specification ClassLoader
The load process checks to see if the class is loaded, the check order is bottom-up, from custom ClassLoader to Bootstrap ClassLoader, and as long as a classloader is loaded, the class is considered loaded. Ensure that only all classloader of this class are loaded once. The order of loading is from top to bottom, which is to try to load this class from the top level.