General

ENUM is another kind of CLASS, but the object for this class is limited defined by designer already; users are not allowed to create one.

It is suitable to use enum when the number of objects of a class is confirmed. Here are some examples:

  • Week: Monday, Tuesday, Wednesday, …, Sunday.
  • Month: Janurary, February, March, …, December.
  • Season: Spring, Summer, Fall, Winter.

Definition before JDK 5.0

Syntax

public class SeasonTest {
public static void main(String[] args) {
Season s1 = Season.SPRING;
System.out.println(s1.getSeasonDesc());
System.out.println(s1.getSeasonName());
System.out.println(s1.toString());
}
}

/* Output
This is Spring.
Spring
Season Name is: Spring, and Season Desc is: This is Spring.
*/
class Season {
private final String seasonName;
private final String seasonDesc;

private Season(String seasonName, String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}

public String getSeasonName(){
return seasonName;
}

public String getSeasonDesc() {
return seasonDesc;
}

public static final Season SPRING = new Season("Spring", "This is Spring.");
public static final Season SUMMER = new Season("Summer", "this is Summer");
public static final Season FALL = new Season("Fall", "this is Fall");
public static final Season WINTER = new Season("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

public class SeasonTest2 {
public static void main(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("..", "...");

public class SeasonTest2 {
public static void main(String[] args) {
System.out.println(Season2.SPRING.toString());
}
}
/* Output
Season Name is: Spring, and Season Desc is: This is Spring
*/
enum Season2{
// 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");

private final String seasonName;
private final String seasonDesc;

private Season2(String seasonName, String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}

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

public class SeasonTest2 {
public static void main(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.

public class SeasonTest2 {
public static void main(String[] args) {
Season2.SUMMER.show();
}
}
/*Output
hello world
*/
interface info{
void show();
}

enum Season2 implements info{
SPRING("Spring", "This is Spring"),
SUMMER("Summer", "This is Summer"),
FALL("Fall", "This is Fall"),
WINTER("Winter", "This is Winter");

public void show(){
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.

public class SeasonTest2 {
public static void main(String[] args) {
Season2.SPRING.show();
Season2.SUMMER.show();
Season2.FALL.show();
Season2.WINTER.show();
}
}
/*Output
hello Spring
hello Summer
hello Fall
hello Winter
*/
interface info{
void show();
}

enum Season2 implements info{
SPRING("Spring", "This is Spring"){
public void show(){
System.out.println("hello Spring");
}
},
SUMMER("Summer", "This is Summer"){
public void show(){
System.out.println("hello Summer");
}
},
FALL("Fall", "This is Fall"){
public void show(){
System.out.println("hello Fall");
}
},
WINTER("Winter", "This is Winter"){
public void show(){
System.out.println("hello Winter");
}
};
...
}

References

atguigu from bilibili

Enum methods from Java8 doc