Posted By

studioevoque on 11/08/10


Tagged

java enum


Versions (?)

Get enum from integer value (ordinal)


 / Published in: Java
 

A simple way to get the enum indicated by its ordinal value.

  1. public class EnumTester {
  2.  
  3. enum Color { RED, GREEN, BLUE };
  4.  
  5. public static void main(String[] args) {
  6.  
  7. int selection = 1;
  8. Color selectedColor = Color.values()[ selection ];
  9.  
  10. System.out.println("Selected color ="+selectedColor);
  11. }
  12. }

Report this snippet  

You need to login to post a comment.