how2examples.com
Home - Introduction to Java Programming

Java Enumerations

Introduced in Java 5, enumerations provide a way of providing groups of named constants.

Defining an Enumeration

Enumerations are defined using the enum keyword. Within an enumeration you can specify a number of identifiers (also known as enumeration constants). Each enumeration constant is implicitly a type of, and public, static member of, the enumeration in which they are declared.

Contents of SimpleEnumExample.java:
public enum SimpleEnumExample{
   A, B, C
}

Unlike in other some other programming languages, Java enumerations are fully-fledged classes. All enumerations automatically extend java.lang.Enum. As enumerations are proper classes they can have the same common features of other classes - constructors, methods and instance variables. One difference between enumerations and other classes is that you don't instantiate (i.e. create) an enum using the new keyword.

Contents of ComplexEnumExample.java:
public enum ComplexEnumExample {
   A, B(18), C(7);

    // enumerations can have instance variables
   private int x;
   
   // enumerations can have overloaded constructors
   ComplexEnumExample() {
      this(20);
   }
   
   ComplexEnumExample(int x) {
      this.x = x;
   }
   
   // enumerations can have methods
   public int getX() {
      return x;
   }
}

A constant within an enumeration can be referred to in a similar as you would refer to a static variable of a class - by prefixing it with the enumeration name. Once you have a reference to a enumeration constant you can invoke methods on it in the same way you would the methods of any Java object.

Code snippet:
SimpleEnumExample s = SimpleEnumExample.A;
ComplexEnumExample c = ComplexEnumExample.B;
System.out.println(s+" "+c+" "+c.getX());
Output from running code snippet:
A B 18

Using Enumerations with Selection Constructs

As with all objects, you can compare enumerations using the equals(Object) method. As there will only be one instance of each constant defined in an enumeration, it is also safe to compare two enumeration references for equality by using the == comparison operator. Enumerations can be used to control switch statements.

Code snippet:
SimpleEnumExample e = SimpleEnumExample.A;
if (e==SimpleEnumExample.A) {
   System.out.println("e==A");
}
if (SimpleEnumExample.A.equals(e)) {
   System.out.println("SimpleEnumExample.A.equals(e)");
}
if (e==SimpleEnumExample.B) {
   System.out.println("e==B");
}
if (SimpleEnumExample.B.equals(e)) {
   System.out.println("SimpleEnumExample.B.equals(e)");
}
Output from running code snippet:
e==A
SimpleEnumExample.A.equals(e)
Code snippet:
SimpleEnumExample e = SimpleEnumExample.C;
switch (e) {
   case A:
      System.out.println("case A");
      break;
   case B:
      System.out.println("case B");
      break;
   case C:
      System.out.println("case C");   
      break;
}
Output from running code snippet:
case C

Enumerations methods

The following methods are common to all enumerations:

Code snippet:
for (ComplexEnumExample e : ComplexEnumExample.values()) {
   System.out.println(e+" "+e.name()+" "+e.ordinal()+" "+e.getX());
}
Output from running code snippet:
A A 0 20
B B 1 18
C C 2 7

Enumeration Collections

The collections API provides specialised implementations - java.util.EnumMap and java.util.EnumSet - for storing enumerations. These enumeration-specific collections are particularly compact and efficient.


See Also