深入理解JVM-类加载及类加载器 (4)

jvm规范允许类加载器在预料某个类将要被使用时就预先加载他,如果在预先加载的过程中遇到了.class文件缺失或者存在错误,类加载器必须在程序首次主动使用该类时才报告错误(LinkageError错误)

如果这个类一直没有被程序主动使用,那么类加载器就不会报告错误

image-20200212204737909

image-20200212171808157

image-20200208223421181

image-20200208223434835

image-20200208223505045

image-20200208223542725

依次执行初始化语句:按照CLass类文件中的顺序加载静态方法和静态代码块。

image-20200208223642407

image-20200208223705030

image-20200208223738483

重点介绍一下上图的概念。举例说明。

在初始化一个接口时,并不会先初始化它的父接口。

// 当一个类初始化时,它实现的接口是不会被初始化的。 public class MyTest5{ public static void main(String[] args){ System.out.println(MyChild5.b); } } interface MyParent5{ public static Thread thread = new Thread(){ // 每次被实例化的时候都会执行下方的代码块。 如果是 static{} 的时候,只会被加载一次。 { System.out.println("myParent5 invoked"); } } } class MyChild5 implements MyParent5{ public static int b = 6; } // 在初始化一个接口时,并不会先初始化它的父接口 public class MyTest5{ public static void main(String[] args){ System.out.println(MyParent5_1.thread); } } interface MyParent5{ public static Thread thread = new Thread(){ { System.out.println("myParent5 invoked"); } } } interface MyParent5_1 extends MyGrandpa5_1{ public static Thread thread = new Thread(){ { System.out.println("myParent5 invoked"); } } }

image-20200208223917870

image-20200208223931946

调用CLassLoader类的loadCLass方法加载一个类,并不是对类的主动使用,不会导致类的初始化。

类加载器 类加载器深度剖析

image-20200208223947354

image-20200208224052752

image-20200208224219186

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpszsp.html