$ javap T.class
Compiled from "T.java"
public final class io.zhudy.web.T extends java.lang.Enum<io.zhudy.web.T> {
public static final io.zhudy.web.T E1;
public static final io.zhudy.web.T E2;
public static io.zhudy.web.T[] values();
public static io.zhudy.web.T valueOf(java.lang.String);
static {};
}
public class T2 {
public static final T2 INSTANCE = new T2();
private T2() {
if (INSTANCE != null) {
throw new AssertionError("实例已存在");
}
}
public static void main(String[] args) throws Exception {
Constructor c = T2.class.getDeclaredConstructor();
Object o = c.newInstance();
System.out.println(o);
}
}
上面這段程式碼編譯後如下:
可以發現常數最後實際上都是被編譯成靜態變數了,Java中靜態變數都是儲存在
Method Area
。