jdk 如何加载类

package com.lysoft.reflction;

/**
 * 测试 JDK 加载类的顺序
 * @author Administrator
 *
 */
public class TestDynamicLoading {

	public static void main(String[] args) {
		
		/**
		 * JDK 并不是一次性把所有的一个类用到的类load进来, 而只有在用到一个类的时候才会把这个类给load进来  
		 * 运行的时候在 虚拟机处 加 -verbose:class 参数可以看到JDK 所加载的类
		 */
		
		new A();
		
		System.out.println("-------------------------------------");
		
		new B();
		
		new C();
		new C();
		
		new D();
		new D();
	}
	
}

class A {}

class B {};

class C {
	
	public C(){
		System.out.println("constraction.");
	};
	
	// static 块只加载一次, 全局只有一份
	static {
		System.out.println("-------CCCCCCCCCCCCCCCCCCCCC--------");
	}
}

class D {
	
	// 动态语句块会加在每个构造方法的前面, 每一次都会执行
	{
		System.out.println("-------DDDDDDDDDDDDDDDDDDDDD--------");
	}
}

猜你喜欢

转载自fishergay.iteye.com/blog/1835449