Learnitweb

Java enum ordinal() method

The ordinal() method of Enum class returns the ordinal of enum constant. It represents the sequence in the enum declaration, where the initial constant is assigned an ordinal of 0. It is very much like array indexes. The ordinal() method returns the order of an enum instance.

Following is the syntax:

public final int ordinal()

public class EnumOrdinalExample {
	public enum Days {
		SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
	}

	public static void main(String args[]) {
		System.out.println("Ordinal of SUNDAY: " + Days.SUNDAY.ordinal());
		System.out.println("Ordinal of MONDAY: " + Days.MONDAY.ordinal());
		System.out.println("Ordinal of TUESDAY: " + Days.TUESDAY.ordinal());
		System.out.println("Ordinal of WEDNESDAY: " + Days.WEDNESDAY.ordinal());
	}
}

Output

Ordinal of SUNDAY: 0
Ordinal of MONDAY: 1
Ordinal of TUESDAY: 2
Ordinal of WEDNESDAY: 3