Easy Tutorial
❮ Java Object Tostring Java String Isempty ❯

Java Enumerations (enum)

Java enumerations are special classes that typically represent a set of constants, such as the four seasons of a year, the twelve months of a year, the seven days of a week, and directions like east, west, north, and south.

Java enum classes are defined using the enum keyword, and individual constants are separated by commas ,.

For example, defining an enum class for colors:

enum Color 
{ 
    RED, GREEN, BLUE; 
}

The above enum class Color has color constants RED, GREEN, and BLUE, representing red, green, and blue, respectively.

Usage example:

Example

enum Color 
{ 
    RED, GREEN, BLUE; 
} 

public class Test 
{ 
    // Output result
    public static void main(String[] args) 
    { 
        Color c1 = Color.RED; 
        System.out.println(c1); 
    } 
}

Executing the above code outputs:

RED

Using enums inside inner classes

Enum classes can also be declared inside inner classes:

Example

public class Test 
{ 
    enum Color 
    { 
        RED, GREEN, BLUE; 
    } 

    // Output result
    public static void main(String[] args) 
    { 
        Color c1 = Color.RED; 
        System.out.println(c1); 
    } 
}

Executing the above code outputs:

RED

Each enum is internally implemented by a Class, and all enum values are public static final.

The above enum class Color is internally implemented as:

class Color
{
     public static final Color RED = new Color();
     public static final Color BLUE = new Color();
     public static final Color GREEN = new Color();
}

Iterating over enum elements

You can use a for loop to iterate over enum elements:

Example

enum Color 
{ 
    RED, GREEN, BLUE; 
} 
public class MyClass { 
  public static void main(String[] args) { 
    for (Color myVar : Color.values()) {
      System.out.println(myVar);
    }
  } 
}

Executing the above code outputs:

RED
GREEN
BLUE

Using enum classes in switch statements

Enum classes are commonly used in switch statements:

Example

enum Color 
{ 
    RED, GREEN, BLUE; 
} 
public class MyClass {
  public static void main(String[] args) {
    Color myVar = Color.BLUE;

    switch(myVar) {
      case RED:
        System.out.println("Red");
        break;
      case GREEN:
        System.out.println("Green");
        break;
      case BLUE:
        System.out.println("Blue");
        break;
    }
  }
}

Executing the above code outputs:

Blue

values(), ordinal(), and valueOf() methods

Enum classes defined by enum implicitly inherit from java.lang.Enum and implement the java.lang.Serializable and java.lang.Comparable interfaces.

The values(), ordinal(), and valueOf() methods are located in java.lang.Enum:

Example

enum Color 
{ 
    RED, GREEN, BLUE; 
} 

public class Test 
{ 
    public static void main(String[] args) 
    { 
        // Output all values
        for (Color c : Color.values()) {
            System.out.println(c + " at index " + c.ordinal());
        }

        // Using valueOf()
        System.out.println(Color.valueOf("RED"));
    } 
}

Executing the above code outputs:

RED at index 0
GREEN at index 1
BLUE at index 2
RED
public static void main(String[] args) 
{ 
    // Call values() 
    Color[] arr = Color.values(); 

    // Iterate over the enum
    for (Color col : arr) 
    { 
        // Display the index
        System.out.println(col + " at index " + col.ordinal()); 
    } 

    // Use valueOf() to return the enum constant, throws IllegalArgumentException if not found
    System.out.println(Color.valueOf("RED")); 
    // System.out.println(Color.valueOf("WHITE")); 
} 

Running the above code outputs:

RED at index 0
GREEN at index 1
BLUE at index 2
RED

Enum Class Members

Enums can have their own variables, methods, and constructors, similar to regular classes. The constructor can only use the private access modifier, so it cannot be called from outside.

Enums can contain concrete methods as well as abstract methods. If an enum class has an abstract method, each instance of the enum must implement it.

Example

enum Color 
{ 
    RED, GREEN, BLUE; 

    // Constructor
    private Color() 
    { 
        System.out.println("Constructor called for : " + this.toString()); 
    } 

    public void colorInfo() 
    { 
        System.out.println("Universal Color"); 
    } 
} 

public class Test 
{     
    // Output
    public static void main(String[] args) 
    { 
        Color c1 = Color.RED; 
        System.out.println(c1); 
        c1.colorInfo(); 
    } 
}

Running the above code outputs:

Constructor called for : RED
Constructor called for : GREEN
Constructor called for : BLUE
RED
Universal Color
❮ Java Object Tostring Java String Isempty ❯