package com.simope.ljm; public class MyEnum { public static void main(String[] args) { System.out.println(Color.BLUE.getIndex()); System.out.println(Color.BLUE.getName()); System.out.println(Color.BLUE.getIndex()); System.out.println(Color.PURPLE.ordinal()); System.out.println(Color.RED.getName(1)); System.out.println(Color.RED.toString()); } } enum Color{ RED("i am red", 1), ORANGE("i am orange", 2), YELLOW("i am yellow", 3), GREEN("i am green", 4), BLUE("i am blue", 5), CYAN("i am cyan", 6), PURPLE("i am purple", 7); private String name; private int index; private Color(String name, int index) { this.name = name; this.index = index; } public String getName(int index) { for (Color c : Color.values()) { if (c.getIndex() == index) { return c.getName(); } } return null; } public String toString() { return this.name + " " + this.index; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } }