Introduction To Enum In Java

Introduction 

 
In this article, we discuss Enum as a new feature of Java.
 

What is Enum?

 
It is a data type containing a fixed set of constants. It can be used for directions (North, South, East and West), for days of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday), etcetera. The enum constants are static and final implicitly. It is available from Java 5. An Enum can be thought of as a class with a fixed set of constants.
 

Important points

 
Some important points about Enum are:
  1. may implement many interfaces but cannot extend any class because it internally extends the Enum class.
  2. can have fields, constructors, and methods
  3. it improves type safety
  4. can be easily used in a switch
  5. can be traversed
Example
  1. class EnumEx1 {  
  2.  public enum Seasonlist {  
  3.   SUMMER,  
  4.   FALL,  
  5.   WINTER,  
  6.   SPRING,  
  7.  }  
  8.  public static void main(String args[]) {  
  9.   for (Seasonlist sl: Seasonlist.values())  
  10.    System.out.println(sl);  
  11.  }  
  12. }  
Output
 
Fig-1.jpg

Values() method of enum

 
The Java compiler internally adds the values() method when it creates an enum. The values() method returns an array containing all the values of the enum.
 
Java compiler generates some code automatically
 
The Java compiler automatically creates a final and static class that extends the Enum.
 

Defining Enum

 
The enum can be defined within or outside the class because it is similar to a class.
 
Example
  1. enum Seasonlist {  
  2.  SUMMER,  
  3.  FALL,  
  4.  WINTER,  
  5.  SPRING,  
  6. }  
  7. class EnumEx2 {  
  8.  public static void main(String args[]) {  
  9.   for (Seasonlist sl: Seasonlist.values())  
  10.    System.out.println(sl);  
  11.  }  
  12. }  
Output
 
fig-2.jpg

Initializing some specific value to constants

 
Constants of an enum can have some initial values, that start from 0, 1, 2, 3 and so on. But we can initialize a specific value of the enum by defining constructors and fields. As stated earlier, an enum contains constructors, methods, and fields.
 
Example: The following example shows the specification of initial value to the enum constants.
  1. class EnumEx3 {  
  2.  enum Seasonlist {  
  3.   SUMMER(5), FALL(10), WINTER(15), SPRING(20);  
  4.   private int val;  
  5.   private Seasonlist(int val) {  
  6.    this.val = val;  
  7.   }  
  8.  }  
  9.  public static void main(String args[]) {  
  10.   for (Seasonlist sl: Seasonlist.values())  
  11.    System.out.println(sl + " " + sl.val);  
  12.  }  
  13. }  
Output
 
fig-3.jpg

Applying enum on a switch statement

 
We can also apply an enum on a switch statement.
 
Example
  1. class EnumEx4 {  
  2.  enum DayList {  
  3.   SUN,  
  4.   MON,  
  5.   TUE,  
  6.   WED,  
  7.   THU,  
  8.   FRI,  
  9.   SAT  
  10.  }  
  11.  public static void main(String args[]) {  
  12.   DayList day = DayList.SUN;  
  13.   switch (day) {  
  14.    case SUN:  
  15.     System.out.println("you are choosing Sunday");  
  16.     break;  
  17.    case MON:  
  18.     System.out.println("You are choosing Monday");  
  19.     break;  
  20.    default:  
  21.     System.out.println("You are choosing other day");  
  22.   }  
  23.  }  
  24. }   
Output
 
fig-4.jpg


Similar Articles