I'm just now realizing their power and utility over using a Constants class... but I'm very interested to know how they're implemented under the hood. They seem to work alot like a static method or static constant in that you just import the file that "owns" the enum and you can make reference to them by using enumName.valueName
. The Javadocs online seem to suggest they're a class
but it seems weird to have an "unnamed" class on demand. (In Java at least...)
Asked
Active
Viewed 9,512 times
14

avgvstvs
- 6,196
- 6
- 43
- 74
2 Answers
19
i believe each instance of the enum is an anonymous final subclass of the enum.
decompile:
public enum Color {
r(0xff0000), g(0x00ff00), b(0x0000ff);
Color(int rgb) {
this.rgb=rgb;
}
final int rgb;
}
and you can see the instances being made:
D:\home\ray\dev\indigoapps\so8032067enumimpl\bin>javap -c Color
Compiled from "Color.java"
public final class Color extends java.lang.Enum{
public static final Color r;
public static final Color g;
public static final Color b;
final int rgb;
static {};
Code:
0: new #1; //class Color
3: dup
4: ldc #16; //String r
6: iconst_0
7: ldc #17; //int 16711680
9: invokespecial #18; //Method "<init>":(Ljava/lang/String;II)V
12: putstatic #22; //Field r:LColor;
15: new #1; //class Color
18: dup
19: ldc #24; //String g
21: iconst_1
22: ldc #25; //int 65280
24: invokespecial #18; //Method "<init>":(Ljava/lang/String;II)V
27: putstatic #26; //Field g:LColor;
30: new #1; //class Color
33: dup
34: ldc #28; //String b
36: iconst_2
37: sipush 255
40: invokespecial #18; //Method "<init>":(Ljava/lang/String;II)V
43: putstatic #29; //Field b:LColor;
46: iconst_3
47: anewarray #1; //class Color
50: dup
51: iconst_0
52: getstatic #22; //Field r:LColor;
55: aastore
56: dup
57: iconst_1
58: getstatic #26; //Field g:LColor;
61: aastore
62: dup
63: iconst_2
64: getstatic #29; //Field b:LColor;
67: aastore
68: putstatic #31; //Field ENUM$VALUES:[LColor;
71: return
public static Color[] values();
Code:
0: getstatic #31; //Field ENUM$VALUES:[LColor;
3: dup
4: astore_0
5: iconst_0
6: aload_0
7: arraylength
8: dup
9: istore_1
10: anewarray #1; //class Color
13: dup
14: astore_2
15: iconst_0
16: iload_1
17: invokestatic #43; //Method java/lang/System.arraycopy:(Ljava/lang/Obj
ect;ILjava/lang/Object;II)V
20: aload_2
21: areturn
public static Color valueOf(java.lang.String);
Code:
0: ldc #1; //class Color
2: aload_0
3: invokestatic #51; //Method java/lang/Enum.valueOf:(Ljava/lang/Class;L
java/lang/String;)Ljava/lang/Enum;
6: checkcast #1; //class Color
9: areturn
}

Ray Tayek
- 9,841
- 8
- 50
- 90
-
2You should only get anonymous derived classes if the enum constants have method definitions (a pair of curlies should do). As ever, the derived classes will have names such as `Color$0`. – Tom Hawtin - tackline Nov 07 '11 at 08:17
0
Enums are statically created when the enum class is first loaded and are immutable.
You must have a constructor in the enum class if you want to assign different values to your enum. After the constructor was finished you cannot change the enums value (immutable as said).
You don't need to use equals() method on enums: the == operator will work just fine.
I have written a tutorial in my blog showing some nice uses of the enum class, that are not trivial from just reading the enum reference.
if you're interested, heres the link. Blog

Gleeb
- 10,773
- 26
- 92
- 135
-
The `public static weekDays fromInt(int value)` method in your blog should be replaced by an `EnumMap`. There are numerous spelling errors there too, as there were here before I fixed them for you. – user207421 Nov 07 '11 at 05:45
-
I won't be doing that but with respect you need a better review process *before* posting you your blog in that case. You have elementary mistakes like 'contractor' for 'constructor' for example. – user207421 Nov 07 '11 at 07:54