Trying to get the associated value from an Enum at runtime in Java

Posted by devoured elysium on Stack Overflow See other posts from Stack Overflow or by devoured elysium
Published on 2010-04-14T13:51:12Z Indexed on 2010/04/14 13:53 UTC
Read the original article Hit count: 122

Filed under:
|

I want to accomplish something like the following (my interest is in the toInt() method). Is there any native way to accomplish this? If not, how can I get the integer associated with an enum value (like in C#) ?

enum Rate {
 VeryBad(1),
 Bad(2),
 Average(3),
 Good(4),
 Excellent(5);

 private int rate;

 private Rate(int rate) {
  this.rate = rate;
 }

 public int toInt() {
  return rate;
 }
}

Thanks

© Stack Overflow or respective owner

Related posts about enum

Related posts about java