public class JavaEnumSwitchCaseExample { enum Margin { TOP, RIGHT, BOTTOM, LEFT } public static void main(String[] args) { System.out.println(getMarginValue(Margin.TOP)); } public static String getMarginValue(Margin margin) { switch (margin) { case TOP: return "1em"; case RIGHT: return "12px"; case BOTTOM: return "1.5em"; case LEFT: return "6px"; default: return null; } } } //end of class
2. using if-else example
public class JavaEnumIfThenExample { public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public static void main(String[] args) { Day theDay = Day.THURSDAY; printDayGreeting(theDay); } public static void printDayGreeting(Day day) { if (day == Day.FRIDAY) System.out.println("TGIF"); else System.out.println("Some other day"); } } //end of class
reference : http://www.devdaily.com/java/using-java-enum-examples-tutorial
0 comments:
Post a Comment