jaav 类动态加载,static 语句块加载一次
package test.java; /** * * @author lj * @printout * [Loaded test.java.A from file:/D:/Users/workspaceReal1/ptservice/build/classes/] ---------------------- [Loaded test.java.B from file:/D:/Users/workspaceReal1/ptservice/build/classes/] [Loaded test.java.C from file:/D:/Users/workspaceReal1/ptservice/build/classes/] static of CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC [Loaded test.java.D from file:/D:/Users/workspaceReal1/ptservice/build/classes/] dynamic of DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD dynamic of DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD */ public class TestDynamicLoading { public static void main(String[] args) { new A();//详细输出 -verbose:class,这个是动态加载。先加载A,再打印,第三句才加载B类。即classLoader 是用到的时候才动态加载类 System.out.println("----------------------"); new B(); new C(); new C(); new D(1); new D(2); } } class A{ } class B{ } class C{ static { //静态语句块,在加载类的时候加载,并且只被加载一次 System.out.println("static of CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); } } class D{ public D(int i){} { //动态语句块,在每次new 对象的时候调用,用的不多。这个与构造方法区别不大,仅当构造方法有参数时,才发现,这个无条件加在每个构造方法前面 System.out.println("dynamic of DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"); } }