«Back to Home

Core Java

Topics

Enum In Java

Enum
 
In Java, Enum is a data type, which holds the fixed set of constants. It can be used for days of the week like Sunday, Monday etc. and directions like North, South etc. The Enum constants are static and final implicitly. It is available from JDK 1.5.
 
Enums can be thought of as the classes, which contains a fixed set of constants.
 
Important points for Java Enum
  • It improves the type safety.

  • It can be easily used in the switch.

  • It can be traversed.

  • It can have the fields, constructors and methods.

  • It may implement many interfaces but cannot extend any class because it internally extends Enum class.
Let’s see an example, given below.
 
Code
  1. public class EnumExample {  
  2.     public enum direction {  
  3.         North, South, East, West  
  4.     }  
  5.     public static void main(String[] args) {  
  6.         for (direction d : direction.values()) {  
  7.             System.out.println(d);  
  8.         }  
  9.     }  
  10. }  
44

Output

45

What is the purpose of values() method in Enum?
 
The compiler internally adds the values() method, when it creates an Enum. The values() method is used to return an array, which contains all the values of the Enum.
 
The compiler internally creates a static and final class that extends the Enum class.
 
What Enum can do in Java

Enum can define within or outside the class because it is similar to a class.
 
Let’s see an example- define outside class, given below.
 
Code
  1. enum Direction {  
  2.     North, South, East, West  
  3. }  
  4. public class EnumExample {  
  5.   
  6.     public static void main(String[] args) {  
  7.         Direction d = Direction.West;  
  8.         System.out.println(d);  
  9.     }  
  10. }  
46

Output

47

Let’s see an example- define inside class, given below.
 
Code
  1. public class EnumExample {  
  2.     enum Direction {  
  3.         North, South, East, West  
  4.     }  
  5.     public static void main(String[] args) {  
  6.         Direction d = Direction.North;  
  7.         System.out.println(d);  
  8.     }  
  9. }  
48

Output

49

Initializing exact values to the enum constants

In Java, The enum constants have initial value which starts from 0, 1, 2, and so on. But we can initialize the exact value to the enum constants with defining fields and constructors. As specified earlier, Enum can have fields, constructors and methods.
 
Let’s see an example of specifying the initial value to Enum constants.
 
Code
  1. public class EnumExample {  
  2.     enum Direction {  
  3.         East(1), West(2), North(3), South(4);  
  4.         private int value;  
  5.         private Direction(int value) {  
  6.             this.value = value;  
  7.         }  
  8.     }  
  9.     public static void main(String args[]) {  
  10.         for (Direction d : Direction.values()) {  
  11.             System.out.println(d + " " + d.value);  
  12.         }  
  13.     }  
  14. }  
50

Output

51

In the example, mentioned above, constructor of Enum type is private. If we don't declare the private compiler internally, it creates the private constructor. We cannot create the object of Enum by new keyword because it contains only private constructors. We can have abstract methods and can give the implementation of these methods.
 
Enum in switch statement

We can apply Enum on switch statement.
 
Let’s see an example of Enum on switch statement, given below.
 
Code
  1. public class EnumExample {  
  2.     enum Direction {  
  3.         East, West, North, South  
  4.     }  
  5.     public static void main(String args[]) {  
  6.         Direction d = Direction.South;  
  7.         switch (d) {  
  8.             case South:  
  9.                 System.out.println("south");  
  10.                 break;  
  11.             case North:  
  12.                 System.out.println("north");  
  13.                 break;  
  14.             default:  
  15.                 System.out.println("other directions");  
  16.         }  
  17.     }  
  18. }  
52

Output

53

Summary

Thus, we learnt that Java Enum is a data type, which holds the fixed set of the constants. It can be used for the days of the week like Sunday, Monday etc. and directions like North, South etc. and how we can use it in Java.