Enum in Java

In this tutorial, we will learn about Enum in Java. In Java Enum is a kind of class and it is used to represent a group of constants. For example days of week and months of the year etc. Enum is only used in case we know all the possible values(constants) on the time of compiling code.

There is enum keyword is available in Java which is used to create Enum. Furthermore, we use comma(,) operator in Enum to separate constants.

Example Program:

enum Days {
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
  THURSDAY, FRIDAY, SATURDAY; 
}

public class ShowDays {
  public static void main(String[] args) {
    Days day = Days.SUNDAY; 
    System.out.println(day);
  }
}

Output:

SUNDAY

Using switch statement with Enum in Java:

enum Days {
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
  THURSDAY, FRIDAY, SATURDAY; 
}

public class ShowDays {
  public static void main(String[] args) {
    Days day = Days.SUNDAY;
    switch(day) {
      case SUNDAY:
        System.out.println("Finally Sunday, Enjoy your Day");
        break;
      case MONDAY:
         System.out.println("Go Back to Work");
        break;
      case TUESDAY:
        System.out.println("Again Boring working day");
        break;
      case WEDNESDAY:
        System.out.println("OMG Still Three days to Sunday");
        break;
      case THURSDAY:
        System.out.println("Thank God Haft week passed");
        break;
      case FRIDAY:
        System.out.println("Sunday is Near.");
        break;
      case SATURDAY:
        System.out.println("It's Saturday Night");
        break;
    }
  }
}

Output:

Finally Sunday, Enjoy your Day

The important thing to note about Enum:

Internally Enum is represented as a class and the constants of Enum represents objects of the class. Here is an example of how the compiler will see our Days Enum which we created in the last example.

class Days
{
     public static final Days SUNDAY = new Days();
     public static final Days MONDAY = new Days();
     public static final Days TUESDAY = new Days();
     public static final Days WEDNESDAY = new Days();
     public static final Days THURSDAY = new Days();
     public static final Days FRIDAY = new Days();
     public static final Days SATURDAY = new Days();
}

Methods in Enum:

There are some methods which are present inside the java.lang.Enum class. These methods are as follow:

  • values(): This method is used to fetch all the constants of Enum.
  • ordinal(): This method helps us to get an index of constant.
  • valueOf(): This method returns the enum constant of String value.

Example Program:

enum Days {
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
  THURSDAY, FRIDAY, SATURDAY; 
}

public class ShowDays {
  public static void main(String[] args) {
    Days week[] = Days.values(); 
    // enum with loop 
     for (Days day : week) 
      {
       System.out.println("No. "+(day.ordinal()+1) + " day is " +day); 
      } 
  }
}

Output:

No. 1 day is SUNDAY
No. 2 day is MONDAY
No. 3 day is TUESDAY
No. 4 day is WEDNESDAY
No. 5 day is THURSDAY
No. 6 day is FRIDAY
No. 7 day is SATURDAY

 

Spread the love
Scroll to Top