Get enum from integer value (ordinal)


/ Published in: Java
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.