Reputation: 39
while reading about the enums. I came through the post, Static enum vs. Non-static enum where outer enums are answered as static implicitly.
I created a enum and when I checked the byte code, enums are made as final but not static.
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
}
please help me understand the if outer enums are static why they are not declared in byte code?
Upvotes: 1
Views: 44
Reputation: 696
The class itself is not static, but all Enum members are inherently declared as public, static, and final.
For info about how to simulate a static class, see this.
Upvotes: 1