Home » Java » The class loading mechanism of Java virtual machin...

The class loading mechanism of Java virtual machine

We usually write a.Java

file, and then through the compiler to generate.Class file, then JVM to run them. This one was JVM how to load a class? A class from top to bottom is how to parse


Java virtual machine and the life cycle of the program, that is, JVM in which case the end of the life cycle:

1., we call Systen.exit () the code

2. program normal execution, the end of

3. program ah, the implementation of the process encountered an exception or error and abnormal termination, usually we call carsh

4. the operating system error caused the JVM process to terminate

class loading, connecting and initializing

Class loading
The loading of the

class is to find and load the binary data of the class into memory (.Class file)


JVM specification allows a class loader in the expected to be used is pre loaded, if in the process of pre loading was encountered in the.Class file is missing or erroneous, the class loader will use the program for the first time in the active class will report an error (LinkageError error).

if the class has not been actively used, the class loader will not report the error.

Java programs can be divided into 2 classes: active use and passive use of

Active use (six)

1. create an instance of the class (new object)

2. accessing static variables of a class or interface, or assigning a
to the static variable of that class
3. call the static method of class

4. initializes a subclass of the class

5. reflected

When the 6.Java virtual machine starts, it is marked as the class

for the startup class

all are not active, are passive use of.

loading refers to the binary data type in the.Class file is read into memory, the data area area method at run time, and then create a java.lang.Class object in the heap area, used to encapsulate data structure in.

in the method area
Class connection
Validation of the

1. class

When the

class is loaded, it enters the connection phase. The connection is to merge the binary data of the class that has been read into memory into the runtime environment of the virtual machine..

The content of the validation of the


Structure checking of 1. types of files

2. semantic checking

3. byte code verification (malicious write.Class, with virus invasion)

4. validation of binary compatibility

2. class preparation,

in the preparation phase, the Java virtual machine allocates memory for the static variables of the class, and sets the default initial value.

For example, the following Simple class. In the run-up to the class, will use the static variables of type int a to allocate 4 bytes of memory space, and gives the default value of 0 for the static variables of type long b allocate 8 bytes of memory space, and gives the default value of 0.

public, class, Simple {
private, int, a = 1;
private static long b;
static {
B = 2;


3. class

inparsing, Java virtualmachine the binary data in the symbolic references with direct references. Also is the symbolic references with a pointer to a pointer to a XXX memory location, this pointer is direct reference to.

Class initialization

in the initialization phase, initialization statement Ja virtual machine, given the initial value of static variables of the class. In the program, the initialization approach of static variables in 2 ways: (1) static variables initialized in a statement (2) to initialize

in static code block

static variables declare statements and static blocks of code are considered as initialization statements of the class, and the Java virtual opportunity executes them in turn according to the initialization statement in the order of class files,.

The initialization step of the


1. if the class has not been loaded and connected, then load and connect
2., if the class has a direct parent class and the parent class has not yet been initialized, initialize the immediate parent class

3. if the initialization statement exists in the class, the initialization statements

are executed in turn

passive use will not lead to class initialization. To achieve all of the Java virtual machine is a must in each interface is tired or Java program for the first time in active use is only beginning to initialize. Call the ClassLoader class (loadClass) method for loading a class, not the active use of the class, the class will not lead to the initialization of the

The active use of a child class in a

program causes the parent class to be initialized, but the active use of the parent class does not initialize the subclass.

The timing of initialization of class

: when the Java virtual machine to initialize a class, its parent requirements have been initialized, but this rule does not apply to the interface. In the initialization of a class does not initialize which implements the interface. In the initialization of an interface, does not initialize its parent interface. Therefore, a parent interface and not because of its sub interface or class initialization and initialization. Only when the static variable program for the first time using a specific interface, the interface will lead to change in the initial.

only when the static or static methods accessed by the program are indeed defined in the current class or the current interface can the active use of.

be considered for the class or interface
The class loader
that comes with the

1.Java virtual machine
Root loader (BootStrap): written in C++, the programmer cannot obtain this class in the Java code. There is no parent loader and no ClassLoader class is inherited.

Extended class loader (Extension): Java code implementation, you can get that class. The parent loader is the root class loader.

The system class loader (System):Java code implementation allows you to get that class. The loader is the extended class loader.

2. user-defined class loader

Inheritance java.lang.ClassLoader class.

, there is no inheritance between them,.

Class class

GetClassLoader () method: if it is a root class loader, the null
is returned
LoadClass () method: loads the specified class

class loader's parent mechanism, custom class loader, runtime package,