enum
Hi,
With method ordinal() in enum we can get the position for that particular constant. But I would like to know, is there any way to get the value when we give the ordinal. Here is the code below.
importstatic java.lang.System.out;
import java.util.*;
enum Day{
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
publicstatic String getDay(Day day){
return day.toString().toUpperCase();
}
publicstatic String getDay(String day){
return day.toUpperCase();
}
publicstaticint ordinal(String day){
return Day.valueOf(day.toUpperCase()).ordinal() + 1;
}
}
publicclass EnumDemo{
publicstaticvoid main(String ... args){
Day[] d = Day.values();
for(Day day : d) out.println("day = "+day+"\t|\t "+
Day.valueOf(day.toString())+" \t|\t "+
Day.ordinal(day.toString()));
}
}
I wrote those methods getDay(Day), getDay(String), ordinal(String) to print the values when we pass the values from command line arguments. But I cut that as it is not relavent here.

