Java Enum (enum)

Java enum is a special class that generally represents a set of constants, such as the 4 seasons of a year, the 12 months of a year, the 7 days of a week, and directions like east, south, west, north, etc.

Java enum classes are defined using the enum keyword, with each constant separated by a comma,to separate them.

For example, define an enum class for colors.

enum Color 
{ 
    RED, GREEN, BLUE; 
} 

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

Usage example:

Example

enum Color
{
    RED, GREEN, BLUE;
}
 
public class Test
{
    // Execute and output the result
    public static void main(String[] args)
    {
        Color c1 = Color.RED;
        System.out.println(c1);
    }
}

Executing the above code produces the output:

RED

Using enums in inner classes

Enum classes can also be declared in inner classes:

Example

public class Test
{
    enum Color
    {
        RED, GREEN, BLUE;
    }
 
    // Execute and output the result
    public static void main(String[] args)
    {
        Color c1 = Color.RED;
        System.out.println(c1);
    }
}

Executing the above code produces the output:

RED

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

The above enum class Color is transformed into the following inner class implementation:

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 statement 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 produces the output:

RED
GREEN
BLUE

Using enum classes in switch

Enum classes are often 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 produces the output:

Blue

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

Enum classes defined with enum inherit the java.lang.Enum class by default and implement the java.lang.Serializable and java.lang.Comparable interfaces.

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

  • values() returns all the values in the enum class.
  • The ordinal() method can find the index of each enum constant, just like an array index.
  • The valueOf() method returns the enum constant of the specified string value.

Example

enum Color
{
    RED, GREEN, BLUE;
}
 
public class Test
{
    public static void main(String[] args)
    {
        // Call values()
        Color[] arr = Color.values();
 
        // Iterate over the enum
        for (Color col : arr)
        {
            // View the index
            System.out.println(col + " at index " + col.ordinal());
        }
 
        // Use valueOf() to return an enum constant; an IllegalArgumentException will be thrown if it does not exist
        System.out.println(Color.valueOf("RED"));
        // System.out.println(Color.valueOf("WHITE"));
    }
}

Executing the above code produces the output:

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

Enum class members

Like ordinary classes, enums can have their own variables, methods, and constructors. Constructors can only use the private access modifier, so they cannot be called externally.

Enums can contain both concrete methods and abstract methods. If an enum class has abstract methods, each instance of the enum class must implement them.

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

Executing the above code produces the output:

Constructor called for : RED
Constructor called for : GREEN
Constructor called for : BLUE
RED
Universal Color
Other extensions