public String getSeasonName(){ return seasonName; }
public String getSeasonDesc() { return seasonDesc; }
publicstaticfinalSeasonSPRING=newSeason("Spring", "This is Spring."); publicstaticfinalSeasonSUMMER=newSeason("Summer", "this is Summer"); publicstaticfinalSeasonFALL=newSeason("Fall", "this is Fall"); publicstaticfinalSeasonWINTER=newSeason("Winter", "this is Winter"); @Override public String toString(){ return"Season Name is: " + seasonName + ", and Season Desc is: " + seasonDesc + "\n"; } }
Here are reasons for qualifying ENUM objects with public static final. We need to access those objects so they are public for sure, and we users are not allowed to use the constructor so that static is the way we can access those objects inside the ENUM class by calling Season.SPRING or Season.FALL for example. As for final, this is because we do not want that users can modify the object including changing the object referred to or reassigning different values inside the object.
Class & Superclass of ENUM Object
publicclassSeasonTest2 { publicstaticvoidmain(String[] args) { System.out.println(Season2.SUMMER.getClass()); System.out.println(Season2.SUMMER.getClass().getSuperclass()); } } /* Output class Season class java.lang.Object */
As shown above, we can clearly see ENUM object before JDK 5.0 is just a normal class.
Definition in JDK 5.0+
Syntax
We will use keyward enum instead of class, and we can omit qualifications on enum objects as well as the duplicated new syntax. For example:
public static final Season Spring = new Season("..", "..."); =>
SPRING("..", "...");
publicclassSeasonTest2 { publicstaticvoidmain(String[] args) { System.out.println(Season2.SPRING.toString()); } } /* Output Season Name is: Spring, and Season Desc is: This is Spring */
enumSeason2{ // The ENUM objects have to be at first. SPRING("Spring", "This is Spring"), SUMMER("Summer", "This is Summer"), FALL("Fall", "This is Fall"), WINTER("Winter", "This is Winter");
public String getSeasonName(){ return seasonName; }
public String getSeasonDesc() { return seasonDesc; }
@Override public String toString(){ return"Season Name is: " + seasonName + ", and Season Desc is: " + seasonDesc; } }
Class & Superclass of ENUM Object
publicclassSeasonTest2 { publicstaticvoidmain(String[] args) { System.out.println(Season2.SUMMER.getClass()); System.out.println(Season2.SUMMER.getClass().getSuperclass()); System.out.println(Season2.SUMMER.getClass().getSuperclass().getSuperclass()); } } /* Output class Season2 class java.lang.Enum class java.lang.Object */
We can find out that the keyward enum makes our class get inherited from Class Enum which is inherited from Class Object.
Frequent ENUM Object Methods
String toString(): Returns the name of this enum constant, as contained in the declaration.
static ENUM_TYPE[] values(): Returns an array of ENUM objects.
static ENUM_TYPE valueOf(String name): Returns the enum constant of the specified enum type with the specified name.
(Deprecated) String name(): Returns the name of this enum constant, exactly as declared in its enum declaration. Use toString() instead since it provides info more human-friendly.
int ordinal(): Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).
ENUM Object Implements Interfaces
Case 1
If we override the method of interface in enum class, different ENUM objects will use the same method.
enumSeason2implementsinfo{ SPRING("Spring", "This is Spring"), SUMMER("Summer", "This is Summer"), FALL("Fall", "This is Fall"), WINTER("Winter", "This is Winter"); publicvoidshow(){ System.out.println("hello world"); } ... }
Case 2
If we override the method of interface inside every ENUM object, different ENUM objects will use the corresponding method which is different with others.