The introduction of enum from jdk1.5 is actually a special class, which is easy to write. JVM automatically encapsulates a lot of things for you.
Package scjp; <br/> public class test {<br/> Public static void main (string ARGs []) {<br/> system. out. println (coffeesize. big = coffeesize. big); <br/>}< br/> Enum coffeesize {<br/> // 8, 10 & 16 are passed to the constructor <br/> big (8), huge (10), overwhelming (16); <br/> coffeesize (INT ounces) {// constructor <br/> This. ounces = ounces; <br/> system. out. println (ounces); <br/>}< br/> private int ounces; // an instance variable <br/> Public int getounces () {<br/> return ounces; <br/>}< br/>
The compiled Enum (coffeesize above) also generates a coffeesize. Class
Decompilation through class assemblyJavap-C coffeesizeGet the following code:
Compiled from "test. java "<br/> final class scjp. coffeesize extends Java. lang. enum {<br/> Public static final scjp. coffeesize big; <br/> Public static final scjp. coffeesize huge; <br/> Public static final scjp. coffeesize overwhelming; <br/> static {}; <br/> code: <br/> 0: New #1; // class scjp/coffeesize <br/> 3: DUP <br/> 4: LDC #16; // string big <br/> 6: iconst_0 <br/> 7: bipush8 <br/> 9: invokespecial #17; // method "<init>" :( ljava/lang/string; ii) v <br/> 12: putstatic #21; // field big: lscjp/coffeesize; <br/> 15: New #1; // class scjp/coffeesize <br/> 18: DUP <br/> 19: LDC #23; // string huge <br/> 21: iconst_1 <br/> 22: bipush10 <br/> 24: invokespecial #17; // method "<init>" :( ljava/lang/string; ii) v <br/> 27: putstatic #24; // field huge: lscjp/coffeesize; <br/> 30: New #1; // class scjp/coffeesize <br/> 33: DUP <br/> 34: LDC #26; // string overwhelming <br/> 36: iconst_2 <br/> 37: bipush16 <br/> 39: invokespecial #17; // method "<init>" :( ljava/lang/string; ii) v <br/> 42: putstatic #27; // field overwhelming: lscjp/coffeesize; <br/> 45: iconst_3 <br/> 46: anewarray #1; // class scjp/coffeesize <br/> 49: DUP <br/> 50: iconst_0 <br/> 51: getstatic #21; // field big: lscjp/coffeesize; <br/> 54: aastore <br/> 55: DUP <br/> 56: iconst_1 <br/> 57: getstatic #24; // field huge: lscjp/coffeesize; <br/> 60: aastore <br/> 61: DUP <br/> 62: iconst_2 <br/> 63: getstatic #27; // field overwhelming: lscjp/coffeesize; <br/> 66: aastore <br/> 67: putstatic #29; // field Enum $ values: [lscjp/coffeesize; <br/> 70: Return <br/> Public int getounces (); <br/> code: <br/> 0: aload_0 <br/> 1: getfield #36; // field ounces: I <br/> 4: ireturn <br/> Public static scjp. coffeesize [] values (); <br/> code: <br/> 0: getstatic #29; // field Enum $ values: [lscjp/coffeesize; <br/> 3: DUP <br/> 4: astore_0 <br/> 5: iconst_0 <br/> 6: aload_0 <br/> 7: arraylength <br/> 8: DUP <br/> 9: istore_1 <br/> 10: anewarray #1; // class scjp/coffeesize <br/> 13: DUP <br/> 14: astore_2 <br/> 15: iconst_0 <br/> 16: iload_1 <br/> 17: invokestatic #55; // method Java/lang/system. arraycopy :( ljava/lang/object; iljava/lang/object; ii) v <br/> 20: aload_2 <br/> 21: areturn <br/> Public static scjp. coffeesize valueof (Java. lang. string); <br/> code: <br/> 0: LDC #1; // class scjp/coffeesize <br/> 2: aload_0 <br/> 3: invokestatic #61; // method Java/lang/enum. valueof :( ljava/lang/class; ljava/lang/string;) ljava/lang/Enum; <br/> 6: checkcast #1; // class scjp/coffeesize <br/> 9: areturn <br/>}< br/>
We can see that each element is actually a public static final instance of the enum class,
There are also:
Public static scjp. coffeesize [] values ();
Public static scjp. coffeesize valueof (Java. Lang. String );
.
Therefore, in some IDES (such as Eclipse), these methods or attributes are automatically prompted later.
You can also use reflection to verify the assembly code again.
In addition, the constructor of Enum can only be private.
The equals method is declared as final, so it cannot override, to ensure that the comparison between different elements returns false, and the same element returns true.
Override of Enum
Public class demo {</P> <p> Public static void main (string... ARGs) {// <--> string ARGs [] <br/> myenum. b. method (); // output: <br/> // B: method () </P> <p >}< br/> }; </P> <p> Enum myenum {<br/> A (1), B (2) {// seems Enum elements must be placed ahead of other fieds and methods <br/> // override <br/> Public void method () {<br/> system. out. println ("B: method ()"); <br/>}< br/>}; <br/> private int value; <br/> myenum (INT value) {<br/> This. value = value; <br/>}</P> <p> // a common method <br/> Public void method () {<br/> system. out. println ("myenum: method ()"); <br/>}< br/> };
Conclusion: Enum can be considered as a special class with syntax requirements. For example, the constructor cannot be public, and the element Declaration must be placed at the beginning of the class. Element is a public static final instance, and enum cannot be declared in method.