主動使用 class時java
被動引用不會初始化類segmentfault
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被加載了,可是沒有被初始化jvm
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); } }
輸出spa
CONST
FinalFieldClass 類沒有由於常量 constString 被引用而初始化,而是直接把CONST 放在常量池中
線程
例子:加載String類3d
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)code
public toString (
)blog
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>函數是帶鎖線程安全的,可能會致使死鎖。