Get all ordinal values using ordinal() for enum in Java
Description
The following code shows how to get all ordinal values using ordinal().
Example
/*from w ww .j a v a 2 s .com*/
enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}
public class Main {
public static void main(String args[]) {
// Obtain all ordinal values using ordinal().
System.out.println("Here are all week constants" + " and their ordinal values: ");
for (Week day : Week.values())
System.out.println(day + " " + day.ordinal());
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »