类加载 在Java代码中,类型的加载、连接与初始化过程都是在程序运行期间完成的。
类加载常见的行为是将磁盘上的class文件加载到内存中
连接将是类与类之间的关系处理好
初始化对一些静态的变量进行赋值
这提供了更大的灵活性,增加了更多的可能性。
类加载深入剖析 加载类的工具,叫做类加载器
Java虚拟机的生命周期 在如下几种情况,Java虚拟机将结束生命周期
执行了System.exit()方法
程序正常执行结束
程序在执行过程中遇到了异常或错误而异常终止
由于操作系统出现错误而导致Java虚拟机进程终止
类的加载、连续、与初始化
加载:查找并加载类的二进制数据
连接
验证:确保被加载的类的正确性
准备:为类的静态变量分配内存,并将其初始化为默认值
解析:把类中的符号引用转换为直接引用
初始化:为类的静态变量赋予正确的初始值。
类的使用和卸载
Java对类的使用方式可以分为两种,主动使用和被动使用。所有的Java虚拟机实现必须在每个类或接口被Java程序首次主动使用 时才初始化他们,这也意味着被动使用不会初始化他们。
主动使用一共有其七种情况:
创建类的实例
访问某个类或接口的静态变量,或者对该静态变量赋值(getstatic,putstatic)
调用类的静态方法(invokestatic)
反射(如Class.forName(“com.test.Test”))
初始化一个类的子类
Java虚拟机启动时被标明为启动类的类(Java Test)
JDK1.7开始提供的动态语言支持:java.lang.invoke.MethodHandle实例的解析结果REF_getStatic,REF_putStatic,REF_invokeStatic句柄对应的类没有初始化,则初始化
除了以上其中情况,其他使用Java类的方式都被看做是对类的被动使用,都不会导致类的初始化。
类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在内存中创建一个java.lang.Class对象(规范未说明Class对象位于哪里,HotSpot虚拟机将其放在了方法区中)用来封装类在方法区内的数据结构(JDK8以后没有方法区了,叫做MetaSpace)
加载.class文件的几种方式:
从本地系统中直接加载
通过网络下载.class文件
从zip,jar等归档文件中加载.class文件
从专有数据库中提取.class文件
将Java源文件动态编译为.class文件
例1 下面给一个类加载的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 public class MyTest1 { public static void main (String[] args) { System.out.println(MyChild1.str); } } class MyParent1 { public static String str = "hello world" ; static { System.out.println("MyParent1 static block" ); } } class MyChild1 extends MyParent1 { public static String st2 = "welcome" ; static { System.out.println("MyChild1 static block" ); } }
这里用到了一个JVM参数,其实我们大多数时候,可能只是调整一下堆大小,其他的参数用得比较少,其实这个参数是有规律可循的。
-XX:+<option>,表示开启option选项 -XX:-<option>,表示关闭option选项 -XX:<option>=value,表示将option选项的值设置为value
例2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public class MyTest2 { public static void main (String[] args) { System.out.println(MyParent2.str); } } class MyParent2 { public static final String str = "hello world" ; public static final short s = 127 ; public static final int i = 0 ; public static final int m = 6 ; static { System.out.println("MyParent2 static block" ); } }
其实这些助记符在rt.jar里都能找到,比如iconst就在com.sun.org.apache.bcel.internal.generic.ICONST里。
例3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class MyTest3 { public static void main (String[] args) { System.out.println(MyParent3.str); } } class MyParent3 { public static final String str = UUID.randomUUID().toString(); static { System.out.println("MyParent3 static code" ); } }
例4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 public class MyTest4 { public static void main (String[] args) { MyParent4[] myParent4s = new MyParent4 [1 ]; System.out.println(myParent4s.getClass()); MyParent4[][] myParent4s1 = new MyParent4 [1 ][1 ]; System.out.println(myParent4s1.getClass()); System.out.println(myParent4s.getClass().getSuperclass()); System.out.println(myParent4s1.getClass().getSuperclass()); System.out.println("=======" ); int [] ints = new int [1 ]; System.out.println(ints.getClass()); System.out.println(ints.getClass().getSuperclass()); char [] chars = new char [1 ]; System.out.println(chars.getClass()); boolean [] booleans = new boolean [1 ]; System.out.println(booleans.getClass()); short [] shorts = new short [1 ]; System.out.println(shorts.getClass()); byte [] bytes = new byte [1 ]; System.out.println(bytes.getClass()); } } class MyParent4 { static { System.out.println("MyParent4 static block" ); } }
例5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class MyTest5 { public static void main (String[] args) { System.out.println(MyChild5.b); } } interface MyParent5 { int a = 5 ; } interface MyChild5 extends MyParent5 { int b = new Random ().nextInt(4 ); }
例6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class MyTest6 { public static void main (String[] args) { Singleton singleton = Singleton.getInstance(); System.out.println("counter:1 " + Singleton.counter1); System.out.println("counter:2 " + Singleton.counter2); } } class Singleton { public static int counter1; private static Singleton singleton = new Singleton (); private Singleton () { counter1++; counter2++; System.out.println(counter1); System.out.println(counter2); } public static int counter2 = 0 ; public static Singleton getInstance () { return singleton; } }