by Dinesh Thakur

Enumeration (enum) was not originally available in Java though it was available in other language like C and C++ but eventually Java realized and introduced Enum on JDK 5 (Tiger) by keyword enum


What is enum in java” simple answer enum is a keyword in java and on more detail term java enum is type like class and interface and can be used to define a set of enum constants. Enum constants are implicitly static and final and you can not change there value once created. Enum in Java provides type-safety and can be used inside switch statement like int variables. Since enum is a keyword you can not use as variable name and since its only introduced in JDK 1.5 all your previous code which has enum as variable name will not work and needs to be re-factored.

Benefits of Enums in Java

1) Enums in Java are type-safe and has there own name-space. It means your enum will have a type for example "Currency" in below example and you can not assign any value other than specified in Enum Constants.
 

public enum Currency {PENNY, NICKLE, DIME, QUARTER};

Currency coin = Currency.PENNY;

coin = 1; //compilation error  


2) Enum has its own name-space.

3) Best feature of Enum is you can use Enum in Java inside Switch statement like int or char primitive data type. We will also see example of using java enum in switch statement in this java enum tutorial.

4) Adding new constants on Enum in Java is easy and you can add new constants without breaking existing code.

 

5) Enums in Java are reference type like class or interface and you can define constructor, methods and variables inside java enum which makes it more powerful than Enum in C and C++

6) Simple enum. The ; after the last element is optional, when this is the end of enum definition.

public enum Color {

WHITE, BLACK, RED, YELLOW, BLUE; //; is optional

}

7) Enum embedded inside a class. Outside the enclosing class, elements are referenced as Outter.Color.RED, Outter.Color.BLUE, etc.

public class Outter {

public enum Color {

   WHITE, BLACK, RED, YELLOW, BLUE

}}

 

8) num that overrides toString method. A semicolon after the last element is required to be able to compile it.

public enum Color {

WHITE, BLACK, RED, YELLOW, BLUE; //; is required here.

@Override public String toString() {

   //only capitalize the first letter

   String s = super.toString();

   return s.substring(0, 1) + s.substring(1).toLowerCase();

}}

 

9) Enum with additional fields and custom constructor. Enum constructors must be either private or package default, and protected or public access modifier is not allowed. When custom constructor is declared, all elements declaration must match that constructor.    

public enum Color {

WHITE(21), BLACK(22), RED(23), YELLOW(24), BLUE(25);

private int code;

private Color(int c) {

   code = c;

}

public int getCode() {

   return code;

}

 

10) Enum that implements interfaces. Enum can implement any interfaces. All enum types implicitly implements java.io.Serializable, and java.lang.Comparable.          

public enum Color implements Runnable {

WHITE, BLACK, RED, YELLOW, BLUE;

public void run() {

   System.out.println("name()=" + name() +

       ", toString()=" + toString());

}}

 

11) A sample test program to invoke this run() method:      

for(Color c : Color.values()) {

c.run();

}

Or,          

for(Runnable r : Color.values()) {

r.run();

}