public enum DirectionEnum { NORTH("N", "North", 1), NORTH_EAST("NE", "North-East", 2), EAST("E", "East", 3), SOUTH_EAST("SE", "South-East", 4), SOUTH("S", "South", 5), SOUTH_WEST( "SW", "South-West", 6), WEST("W", "West", 7), NORTH_WEST("NW", "North-West", 8); private final String code; private final String description; private final short id; private DirectionEnum(String code, String description, int id) { this.code = code; this.description = description; this.id = (short) id; } public String getCode() { return code; } public String getDescription() { return description; } public short getId() { return id; } public static DirectionEnum findById(int id) { for (DirectionEnum dir : DirectionEnum.values()) { if (dir.getId() == id) { return dir; } } return null; } }
public enum VehicleClassEnum { H("Heavy"), L("Light"), N("Not Specified"); private String description; private VehicleClassEnum(String description) { this.description = description; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
import java.util.Vector; import java.util.Enumeration; public class EnumerationTester { public static void main(String args[]) { System.out.println(VehicleClassEnum.valueOf("H")); System.out.println(VehicleClassEnum.valueOf("H").toString()); System.out.println(VehicleClassEnum.valueOf("H").getDescription()); System.out.println(VehicleClassEnum.valueOf(VehicleClassEnumTest.class, VehicleClassEnum.L.getDescription())); System.out.println(VehicleClassEnum.values()); for (VehicleClassEnum classEnum : VehicleClassEnum.values()) { System.out.println("VehicleClassEnum:" + classEnum + ", VehicleClassEnum.toStirng()" + classEnum.toString() + ", VehicleClassEnum.name():" + classEnum.name() + ", VehicleClassEnum.ordinal():" + classEnum.ordinal() + ", getDeclaringClass:" + classEnum.getDeclaringClass() + ", getDescription():" + classEnum.getDescription() + ", compareTo:" + classEnum.compareTo(VehicleClassEnum.L)); } Enumeration days; Vector dayNames = new Vector(); dayNames.add("Sunday"); dayNames.add("Monday"); dayNames.add("Tuesday"); dayNames.add("Wednesday"); dayNames.add("Thursday"); dayNames.add("Friday"); dayNames.add("Saturday"); days = dayNames.elements(); while (days.hasMoreElements()) { System.out.println(days.nextElement()); } } }
---------------OUTPUT---------------
H H Heavy Light [LVehicleClassEnum;@3ce53108 VehicleClassEnum:H, VehicleClassEnum.toStirng()H, VehicleClassEnum.name():H, VehicleClassEnum.ordinal():0, getDeclaringClass:class VehicleClassEnum, getDescription():Heavy, compareTo:-1 VehicleClassEnum:L, VehicleClassEnum.toStirng()L, VehicleClassEnum.name():L, VehicleClassEnum.ordinal():1, getDeclaringClass:class VehicleClassEnum, getDescription():Light, compareTo:0 VehicleClassEnum:N, VehicleClassEnum.toStirng()N, VehicleClassEnum.name():N, VehicleClassEnum.ordinal():2, getDeclaringClass:class VehicleClassEnum, getDescription():Not Specified, compareTo:1 Sunday Monday Tuesday Wednesday Thursday Friday Saturday