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.


// w w  w.jav  a2s .  c om

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.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable