写一个简单的枚举类
public enum EnumTest {
RED("red","红色的"),
GREEN,
Blue;
public final String key;
public final String value;
private EnumTest(String key, String value)
{
this.key = key;
this.value = value;
}
private EnumTest()
{
this.key = "默认key";
this.value = "默认value";
}
public void showParentFields()
{
System.out.println(this.name());
System.out.println(this.ordinal());
}
public void showFields()
{
System.out.println(this.key);
System.out.println(this.value);
}
}
使用javap -c xxx.class反编译
D:\MyJava\Test\bin>javap -c EnumTest.class
Compiled from "EnumTest.java"
public final class EnumTest extends java.lang.Enum<EnumTest> {
public static final EnumTest RED;
public static final EnumTest GREEN;
public static final EnumTest Blue;
public final java.lang.String key;
public final java.lang.String value;
static {};
Code:
0: new #1 // class EnumTest
3: dup
4: ldc #17 // String RED
6: iconst_0
7: ldc #18 // String red
9: ldc #20 // String 红色的
11: invokespecial #22 // Method "<init>":(Ljava/lang/Strin
g;ILjava/lang/String;Ljava/lang/String;)V
14: putstatic #26 // Field RED:LEnumTest;
17: new #1 // class EnumTest
20: dup
21: ldc #28 // String GREEN
23: iconst_1
24: invokespecial #29 // Method "<init>":(Ljava/lang/Strin
g;I)V
27: putstatic #32 // Field GREEN:LEnumTest;
30: new #1 // class EnumTest
33: dup
34: ldc #34 // String Blue
36: iconst_2
37: invokespecial #29 // Method "<init>":(Ljava/lang/Strin
g;I)V
40: putstatic #35 // Field Blue:LEnumTest;
43: iconst_3
44: anewarray #1 // class EnumTest
47: dup
48: iconst_0
49: getstatic #26 // Field RED:LEnumTest;
52: aastore
53: dup
54: iconst_1
55: getstatic #32 // Field GREEN:LEnumTest;
58: aastore
59: dup
60: iconst_2
61: getstatic #35 // Field Blue:LEnumTest;
64: aastore
65: putstatic #37 // Field ENUM$VALUES:[LEnumTest;
68: return
public void showParentFields();
Code:
0: getstatic #52 // Field java/lang/System.out:Ljava/
io/PrintStream;
3: aload_0
4: invokevirtual #58 // Method name:()Ljava/lang/String;
7: invokevirtual #62 // Method java/io/PrintStream.printl
n:(Ljava/lang/String;)V
10: getstatic #52 // Field java/lang/System.out:Ljava/
io/PrintStream;
13: aload_0
14: invokevirtual #68 // Method ordinal:()I
17: invokevirtual #72 // Method java/io/PrintStream.printl
n:(I)V
20: return
public void showFields();
Code:
0: getstatic #52 // Field java/lang/System.out:Ljava/
io/PrintStream;
3: aload_0
4: getfield #42 // Field key:Ljava/lang/String;
7: invokevirtual #62 // Method java/io/PrintStream.printl
n:(Ljava/lang/String;)V
10: getstatic #52 // Field java/lang/System.out:Ljava/
io/PrintStream;
13: aload_0
14: getfield #44 // Field value:Ljava/lang/String;
17: invokevirtual #62 // Method java/io/PrintStream.printl
n:(Ljava/lang/String;)V
20: return
public static EnumTest[] values();
Code:
0: getstatic #37 // Field ENUM$VALUES:[LEnumTest;
3: dup
4: astore_0
5: iconst_0
6: aload_0
7: arraylength
8: dup
9: istore_1
10: anewarray #1 // class EnumTest
13: dup
14: astore_2
15: iconst_0
16: iload_1
17: invokestatic #78 // Method java/lang/System.arraycopy
:(Ljava/lang/Object;ILjava/lang/Object;II)V
20: aload_2
21: areturn
public static EnumTest valueOf(java.lang.String);
Code:
0: ldc #1 // class EnumTest
2: aload_0
3: invokestatic #84 // Method java/lang/Enum.valueOf:(Lj
ava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;
6: checkcast #1 // class EnumTest
9: areturn
}
翻译成代码如下:
public final class EnumTest extends java.lang.Enum<EnumTest> {
//枚举变量自动补上了访问修饰符
public static final EnumTest RED;
public static final EnumTest GREEN;
public static final EnumTest Blue;
public final java.lang.String key;
public final java.lang.String value;
//自动补上了初始化的静态代码块
static {
RED = new EnumTest("red","红色的");
GREEN = new EnumTest();
Blue = new EnumTest();
VALUES = new EnumTest[]{RED,GREEN,Blue};
}
public void showParentFields()
{
System.out.println(this.name());
System.out.println(this.ordinal());
}
public void showFields()
{
System.out.println(this.key);
System.out.println(this.value);
}
//比源码多出来的方法,反编译Enum类也没找到
public static EnumTest[] values()
{
EnumTest tmp = new EnumTest[VALUES.length];
tmp = System.arraycopy(VALUES,0,tmp,VALUES.length);
return tmp;
}
//比源码多出来的方法,来源于父类Enum
public static EnumTest valueOf(java.lang.String param)
{
return Enum.valueOf(param);
}
}
可以看到,经过编译后对源码加了很多内容