Friday, 2 May 2014

Enum : ordinal : Get the Position of enum constant

public final int ordinal()
Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).

enum Day{
 MON,
 TUE,
 WED,
 THU,
 FRI,
 SAT,
 SUN;
 
 public static void main(String args[]){
  for(Day d1 : Day.values()){
   System.out.print("Ordinal value of " + d1 + " is ");
   System.out.println(d1.ordinal());
  }
 }
}

Output
Ordinal value of MON is 0
Ordinal value of TUE is 1
Ordinal value of WED is 2
Ordinal value of THU is 3
Ordinal value of FRI is 4
Ordinal value of SAT is 5
Ordinal value of SUN is 6



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment