Class loading process to resolve

Why study class loading the whole process?
- help to understand the process of running JVM
- a better understanding of the dynamics of java, (antipyretic deployment, dynamic loading), increase the flexibility of the program.
- the JVM loads class files into memory, and verify the data, and parsing initialization, Java JVM type of process can be directly used finally formed.

• class loading mechanism
Here Insert Picture Description
1, loading
the byte code class file is loaded into memory contents, and static data which converts into run-time data structure in the method area, a representative of this class java.lang.Class objects generated in the heap , as a method of accessing the class data entry region.
2, connected
Java class of binary code will be merged into the process in the running state of the JVM
(1) verification
to ensure compliance with the information loaded class JVM specification, no security issues.
(2) Preparation
formally allocate memory and set the stage class variable initial value for class variables (static variables), this memory will be allocated in the process zone
(3) resolved
symbols virtual machine constant pool references replacement process is directly referenced ()
3, initialization
- initialization phase is performed during the class constructor (the clinit) method. Class constructor (the clinit) is assigned by the operation of the compiler to automatically collect all classes and static class variables block of statements (static block) in the statement of merger
- when initializing a class, if the parent class is also found not been initialized, you need to initialize the departure of its parent
- the opportunity to ensure a virtual class () method is correctly locked in a multithreaded environment and synchronous

Divided into: active reference to the class (class initialization must happen), passive reference to the class (class initialization does not occur)
(1) active references
- static member class is called (except final constants), and - a new class of objects static methods
- using java.lang.reflect package approach to reflect the class call
- when the virtual machine is powered on, java Hello, it will initialize the Hello class. It means first start the class where the main method
- the father of his class when a class is initialized, if the parent class is not initialized, the first initializes

(2) Passive reference
- When accessing a static field, only the real statement of this field will be initialized class • static variables referenced by the parent class subclass does not lead to a subclass initialization

- defined by an array of class references, does not trigger this type of initialization
- constant references to such does not trigger initialization (constants at compile it into the constant pool of the class call)
example of
the process of class loading:

package 测试类加载全过程;
public class Demo01 {
	static {
		System.out.println("静态初始化Demo01");
	}
	public static void main(String[] args) {
		System.out.println("Demo01的main方法");
		A a = new A();
		System.out.println(A.width);
		A a2 = new A();//类加载初始化只会有一次
		
	}
}
class A extends A_Father{
	public static int width = 100;//静态变量=静态域
	
	static {
		System.out.println("静态初始化A");
		width = 300;
	}
	public A(){
		System.out.println("创建A类的对象");
	}
}
class A_Father{
	static {
		System.out.println("初始化父类A_Father");//当初始化一个类,如果其父类没有被初始化,则先会初始化他的父类 
	}

// execution results:
• static initialization Demo01
main method • Demo01 of
• initialize the parent class A_Father
• A static initialization
• creating an object of class A
• 300
• creating an object of class A

public class Demo01 {
	public static void main(String[] args) {
		System.out.println(A.max);
		
	}
}
class A extends A_Father{
	public static final int max = 300;
	//除了final常量  final修饰的常量不会发生类的初始化      
	//调用类的静态成员()和静态方法 会发生类的初始化
	
	static {
		System.out.println("静态初始化A");
	}
	public A(){
		System.out.println("创建A类的对象");
	}
}
class A_Father{
	static {
		System.out.println("初始化父类A_Father");//当初始化一个类,如果其父类没有被初始化,则先会初始化他的父类 
	}
}

Implementation of the results:
300

public class Demo01 {
	static {
		System.out.println("静态初始化Demo01");
	}
	public static void main(String[] args) throws Exception {
		//主动引用
		new A();
		//被动引用 final常量
		System.out.println(A.max);
		//反射 主动引用
		Class.forName("ClassTest.A");
		//通过数组定义类引用,不会触发此类的初始化 被动引用
		A[]  as = new A[10];		
		//当访问一个静态域时,只有真正声明这个域的类才会被初始化
		// 通过子类引用父类的静态变量,不会导致子类初始化
		System.out.println(B.width);//B不会初始化
	}
}
class A{
	public static int width = 100;
	public static final int max = 300;//调用类的静态成员(除了final常量)和静态方法 不会发生类的初始化
	
	static {
		System.out.println("静态初始化A");
	}
	public A(){
		System.out.println("创建A类的对象");
	}
}
class B extends A{//A的子类
	static {
		System.out.println("静态初始化B");
	}
}

The results:
static initialization Demo01
static initialization A
to create an object of class A
300
100

Published 10 original articles · won praise 0 · Views 96

Guess you like

Origin blog.csdn.net/xiaobao1352/article/details/104211336