| Author |
enum in Tiger
|
Bilal Al-Sallakh
Greenhorn
Joined: Jan 28, 2005
Posts: 20
|
|
Here are my note about enums in Tiger: Thanks to "Java 1.5 Tiger: A Developer Notebook" 1- Simple definition enum E {V1, V2, ..., Vn} n >= 0 It's a special class that contains public static final instances of itself. 2- E above implicitly extends java.lang.Enum. The later is not an enum, and can't be extended explicitly with class E extends java.lang.Enum {} 3- E constructors are implicitly private, but can be explicit 4- E is effectively final but can't be declared final nor abstract! 5- The values are static references so they can be compared with == 6- E implments java.lang.Comparable (Vi < V(i+1)) 7- E implicitly override toString(): Vi.toString = "Vi", but you can specify another override. 8- E contains: public static E valueOf(String) which (should) complements toString 9- E contains: public int ordinal() which gives the value order (0-based) 10- E contains: public static E[] values() 11- Inner (member) Enums are implicitly static, but can be declared explicitly. 12- You can switch on an enum veriable: E v = E.V1; switch(v) { case V1: case V2: default: } you can write: case E.V1 If (the enum and the switch are in the same compilation unit) a jump table is created and indexed by v.ordinal(); else the switch is converted to an if-then-else; 13- EnumMap<? extends Enum, String> maps enums to string efficiently created with EnumMap<E, String> m = new EnumMap<E, String>(E.class); 14- EnumSet contains: public static EnumSet allOf(Class elementType); public static EnumSet complmentOf(EnumSet e); public static EnumSet copyOf(Collection c); public static EnumSet of(E... e); public static EnumSet range(E from, E to); 15- Enums can contain constructors, methods and veriables enum E { V1(..), V2(..), ..., Vn(..); E(..){}; variables; methods; } 16- Enums can implment interfaces 17- Enums cannot be local classes (defined within a method) 18- Each enum value can override the enum with an ananymous class enum E { V1(..) { f(){} }, V2 { f(){} }; E(..){} E() {} f(){} } But as in the visitor pattern, it's not apporved: enum E { V1(..), V2; E(..){} E() {} f(){ switch (this) { case V1: case V2: } } }
|
 |
Joyce Lee
Ranch Hand
Joined: Jul 11, 2003
Posts: 1392
|
|
Hi Bilal, Thanks for sharing your notes with us. 12- You can switch on an enum veriable: E v = E.V1; switch(v) { case V1: case V2: default: } you can write: case E.V1 By qualifying an enumeration constant in the case statement, i.e. E.V1, will result in compilation error. Joyce
|
 |
Bilal Al-Sallakh
Greenhorn
Joined: Jan 28, 2005
Posts: 20
|
|
|
I exactly meant that but wrote you can instead of you can't!
|
 |
 |
|
|
subject: enum in Tiger
|
|
|