Enum class implicitly implements the Comparable interface to enforce a natural ordering among constants. The natural order is based on the order in which the enum constants are declared. When you create an enum, each of its constants is implicitly comparable to the others.

public enum Day {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY;
}

In the above enum class, natural order is: SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; they have corresponding position starting from 0 for SUNDAY, 1 for MONDAY and continue on. You can use ordinal() enum method to return the ordinal of this enumeration constant.

public static void main(String[] args) {
	System.out.println(Day.MONDAY.ordinal());
}
1

The compareTo() method overridden from the Comparable interface uses these ordinal values to compare the constants.

public static void main(String[] args) {
	Day day1 = Day.MONDAY;
	Day day2 = Day.WEDNESDAY;
	
	System.out.println(day1.compareTo(day2));
}
-1

It compares the ordinal values of day1 and day2. In this case, day1 MONDAY has a ordinal value of 1; day2 WEDNESDAY has a ordinal value of 3. Since is less than , the compareTo() method returns a negative value -1.

Traverse enum constants

The Java compiler provides a static values() method for all enum types, you don’t need to define this method as it is implicitly provided. It returns an array containing all the constants of the enum type, in the order they were declared. You can traverse all the enum constants using a loop.

enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
 
public class Demo {
    public static void main(String[] args) {
        // using the values() method
        Day[] days = Day.values();
        
        // traverse all the constants of the enum
        for (Day day : days) {
            System.out.println(day);
        }
    }
}
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY

You can access a particular element using its index.

Day monday = days[1];

Working with switch statement

Conditional statements like if and switch are useful for executing different task for each choice. Below is are examples of using switch case statement for enums.

public class Demo {
    public static void main(String[] args) {
        for (Day day : Day.values()) {
            switch (day) {
                case MONDAY:
                    System.out.println("Start of the work week.");
                    break;
                case FRIDAY:
                    System.out.println("End of the work week.");
                    break;
                case SUNDAY:
                    System.out.println("Rest day.");
                    break;
                default:
                    System.out.println(day);
            }
        }
    }
}
enum Cars {
	RUNNING,
	FAILED,
	PENDING,
	SUCCESS;
}
 
public class Demo {
	public static void main(String args[]) {
		Cars c;
		c = Cars.RUNNING;
		
		switch (c) {
		case RUNNING:
			System.out.println("Task is Running.");
			break;
		case FAILED:
			System.out.println("Task running failed.");
			break;
		case PENDING:
			System.out.println("Task is pending.");
			break;
		case SUCCESS:
			System.out.println("Task running successed.");
		}
	}
}

Back to parent page: Java Standard Edition (Java SE) and Java Programming

Web_and_App_DevelopmentProgramming_LanguagesJavaEnumNatural_OrderingComparable