by Dinesh Thakur Category: Introduction to Java

Enumeration (enum) was not originally available in Java though it was available in other language like C and C++ but eventually Java realized and added to version 5 of Java were the safe type enumerations (Type Safe Enums), which allows you to create enums in Java, just as they exist in other technologies. It is defined by the reserved word enum, and each item in the enumeration is represented by an object of the same type as the Enum. The definition of an enumeration is no more than the definition of a special kind of class. If they were in another package, they should be imported like any other normal classes.

"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. It is not necessary that the set of constants in an enum type stay fixed for all time. Enum in Java provides type-safety and can be used when we know all possible values at compile time. 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.

publicclass 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.
          @OverridepublicString 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);
         privateint code;
           private Color(int c){
              code = c;
           }

publicint 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;
         publicvoid 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();
}





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.