$ javap T.class Compiled from "T.java" public final class io.zhudy.web.T extends java.lang.Enum { 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
。