Java Data Type How to - Get all ordinal values using ordinal()








Question

We would like to know how to get all ordinal values using ordinal().

Answer

enum Week {/*from ww  w  .ja va  2  s .c o m*/
  Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}

public class MainClass {
  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.