Java Data Type Tutorial - Java Enum.ordinal()








Syntax

Enum.ordinal() has the following syntax.

public final int ordinal()

Example

In the following code shows how to use Enum.ordinal() method.

/*from  w ww . jav a 2  s .  c o m*/

enum Tutorial {
   CSS(400), HTML(250),Java(325);
  
   int price;
   Tutorial(int p) {
      price = p;
   }
   int showPrice() {
      return price;
   } 
}

public class Main {

   public static void main(String args[]) {

     System.out.println("CellPhone List:");
     for(Tutorial m : Tutorial.values()) {
        System.out.println(m + " costs " + m.showPrice() + " dollars");
     }

     Tutorial ret = Tutorial.CSS;
     System.out.println("The ordinal is = " + ret.ordinal());
     System.out.println("TutorialName = " + ret.name());                      
   }
}

The code above generates the following result.