主动使用 class时
被动引用不会初始化类
package com.mousycoder.mycode.thinking_in_jvm; /** * @version 1.0 * @author: mousycoder * @date: 2019-09-02 16:29 */ public class Parent { static { System.out.println("Parent init"); } public static int v = 100; } package com.mousycoder.mycode.thinking_in_jvm; import sun.jvm.hotspot.memory.ParNewGeneration; /** * @version 1.0 * @author: mousycoder * @date: 2019-09-02 16:29 */ public class Child extends Parent { static { System.out.println("Child init"); } } package com.mousycoder.mycode.thinking_in_jvm; /** * @version 1.0 * @author: mousycoder * @date: 2019-09-02 16:32 */ public class UseParent { public static void main(String[] args) { System.out.println(Child.v); } }
虚拟机参数 -XX:+TraceClassLoading
输出
[Loaded com.mousycoder.mycode.thinking_in_jvm.Parent from file:/Users/mousycoder/My/code/mycode/target/classes/] [Loaded com.mousycoder.mycode.thinking_in_jvm.Child from file:/Users/mousycoder/My/code/mycode/target/classes/] Parent init 100 [Loaded java.lang.Shutdown from /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar] [Loaded java.lang.Shutdown$Lock from /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar]
表示 Child被加载了,但是没有被初始化
final 常量不会引起类初始化
package com.mousycoder.mycode.thinking_in_jvm; /** * @version 1.0 * @author: mousycoder * @date: 2019-09-02 16:41 */ public class FinalFieldClass { public static final String constString = "CONST"; static { System.out.println("FinalFieldClass init"); } } package com.mousycoder.mycode.thinking_in_jvm; public class UseFinalField { public static void main(String[] args) { System.out.println(FinalFieldClass.constString); } }
输出
CONST
FinalFieldClass 类没有因为常量 constString 被引用而初始化,而是直接把CONST 放在常量池中
例子:加载String类
package com.mousycoder.mycode.thinking_in_jvm; import java.lang.reflect.Method; import java.lang.reflect.Modifier; /** * @version 1.0 * @author: mousycoder * @date: 2019-09-03 13:38 */ public class StringTest { public static void main(String[] args) throws ClassNotFoundException { Class clzStr = Class.forName("java.lang.String"); Method[] ms = clzStr.getDeclaredMethods(); for (Method m : ms) { String mod = Modifier.toString(m.getModifiers()); System.out.println(mod + " " + m.getName() + " ( "); Class<?>[] ps = m.getParameterTypes(); if (ps.length == 0 ){ System.out.println(')'); } for (int i = 0; i < ps.length; i++) { char end =i==ps.length-1 ? ')':','; System.out.println(ps[i].getSimpleName() + end); } System.out.println(); } } }
输出
``
public equals (
Object)
public toString (
)
public hashCode (
)
public volatile compareTo (
Object)
public compareTo (
String)
public indexOf (
String,
int)`
``
保证加载的字节码合法
为类分配对应的内存空间,并设置初始值,此阶段不会有 java 代码执行
类型 | 默认初始值 |
---|---|
int | 0 |
long | 0L |
short | (short)0 |
char | u0000 |
boolean | false |
reference | null |
float | 0f |
double | 0f |
将类、接口、字段、方法的符号引用(字面量的引用)转成直接引用(找到方法中表的位置)
执行类的初始化方法<clinit>,此方法由编译器自动生成的,由类静态成员的赋值语句以及static 语句块合并产生
其中<clinit>函数是带锁线程安全的,可能会导致死锁。