在阅读有关枚举时。我来过这个职位, Static enum vs. Non-static enum 外部枚举被隐式地回答为静态。
我创建了一个枚举,当我检查字节码时,枚举被定为最终的,但不是静态的。
final class practice.Enums extends java.lang.Enum<practice.Enums> {
public static final practice.Enums FRIEND;
static {};
Code:
0: new #1 // class practice/Enums
3: dup
4: ldc #12 // String FRIEND
6: iconst_0
7: invokespecial #13 // Method "<init>":(Ljava/lang/String;I)V
10: putstatic #17 // Field FRIEND:Lpractice/Enums;
13: iconst_1
14: anewarray #1 // class practice/Enums
17: dup
18: iconst_0
19: getstatic #17 // Field FRIEND:Lpractice/Enums;
22: aastore
23: putstatic #19 // Field ENUM$VALUES:[Lpractice/Enums;
26: return
public static practice.Enums[] values();
Code:
0: getstatic #19 // Field ENUM$VALUES:[Lpractice/Enums;
3: dup
4: astore_0
5: iconst_0
6: aload_0
7: arraylength
8: dup
9: istore_1
10: anewarray #1 // class practice/Enums
13: dup
14: astore_2
15: iconst_0
16: iload_1
17: invokestatic #27 // Method java/lang/System.arraycopy:(Ljava/lang/Object;ILjava/lang/Object;II)V
20: aload_2
21: areturn
public static practice.Enums valueOf(java.lang.String);
Code:
0: ldc #1 // class practice/Enums
2: aload_0
3: invokestatic #35 // Method java/lang/Enum.valueOf:(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;
6: checkcast #1 // class practice/Enums
9: areturn
}
请帮助我了解外部枚举是否是静态的,为什么不在字节码中声明它们?