Java Enums

Java Enums is a special java class that defines a collection of constants. Constants are the values that are fixed and cannot be changed. Constants are similar to final variables. Enum is referred to enumeration of a set of named constants. Enums are similar to classes and can have their own methods and attributes other than constants.

Create Java Enums

The keyword enum is used to create java enums. The keyword is followed by an identifier that is the name of the enum. Curly braces {} are added after the enum name. The curly braces contain the different constants. Note that constants name should be written in upper case letters.

The syntax for creating an enum is:

An enum can have multiple elements. Each element is separated by a comma. An enum can defined inside or outside a class. Enums can also be defined without a class. In this case, main() method is called inside the enum. Here is a simple example that creates an enum Days inside a class. The enum Days contains the names of the days of week.

// File name is Example.java
public class Example { 
  enum Days {     // Define enum
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
  }

  public static void main(String[] args) { 
    System.out.println("The enum Days has been created.");
  } 
}

The output of the above code after compilation is:

Access Enum Constants

Each java enum constant is an object of type enum. The enum constants are public, static and final, that means they cannot be overridden. The dot operator (.) is used to access enum constants. The enum name is followed by dot operator and that is followed by constant name. The syntax is:

The example below shows how to access and print the java enum constants. The enum is first stored in a variable of type enum and then printed.

// File name is Example.java
public class Example { 
  enum Days {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
  }

  public static void main(String[] args) { 
    Days monday = Days.MONDAY;
    System.out.println(monday);
  } 
}

The output of the above code after compilation is:

Access all Enum Constants

The constants of an enum can also be accessed using a for loop. The java enum class has a method called values(). This method gets all the values of constants of an enum. Here is an example that shows how to access all constants of an enum:

// File name is Days.java
enum Days {
  MONDAY,
  TUESDAY,
  WEDNESDAY,
  THURSDAY,
  FRIDAY,
  SATURDAY,
  SUNDAY;
  public static void main(String[] args) { 
    for (Days cons : Days.values()) {
      System.out.println(cons);
    }
  } 
}

In the above code, enum is defined without a class. The name of java file should be same as that of the enum. The output of the above code after compilation is:

Java Enums in Switch statements

Java Enums are used to handle switch statements. For example in the code below, an enum is defined outside the class. The value of enum constant is given in the switch statement as a parameter.

// File name is Example.java
enum Days {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
  }


public class Example { 
  public static void main(String[] args) {
    Days today = Days.TUESDAY; 
                
    switch(today) {
      case MONDAY:
        System.out.println("Today is Monday");
        break;
      case TUESDAY:
        System.out.println("Today is Tuesday");
        break;
      case WEDNESDAY:
        System.out.println("Today is Wednesday");
        break;
      case THURSDAY:
        System.out.println("Today is Thursday");
        break;
      case FRIDAY:
        System.out.println("Today is Friday");
        break;
      case SATURDAY:
        System.out.println("Today is Saturday");
        break;
      case SUNDAY:
        System.out.println("Today is Sunday");
        break;
    }
  }
}

The output of the above code after compilation is:

Index of Constants

Each constant in an enum has an index. Programmer can find that index by using the method ordinal() provided by the java enum class. The below code shows how to extract indices of constants in an enum.

// File name is Example.java
public class Example { 
  enum Days {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
  }

  public static void main(String[] args) { 
    Days day[] = Days.values();
  
        for (Days cons : day)
        {
            System.out.println(cons + " is at index "
                             + cons.ordinal());
        }
  } 
}

Similar to arrays, the index of enum constants starts at 0. The output of the above code after compilation is:

Enum Constructors

Just like classes, java enums can have constructors too. The enum constructor is called with each constant. The below example shows the concept of enum constructors:

// File name is Example.java
enum Days {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY;
  
    private Days()
    {
        System.out.println("Constructor called for : " +
        this.toString());
    }
 
}
  
public class Example
{    
    // Driver method
    public static void main(String[] args)
    {
        Days d1 = Days.TUESDAY;
        System.out.println(d1);
    }
}

The output of the above code after compilation is: