#
enum Shrubbery { GROUND, CRAWLING, HANGING } public static void main(String[] args) { for (Shrubbery s : Shrubbery.values()) { System.out.println(s + ", ordinal: " + s.ordinal()); System.out.println(s.compareTo(Shrubbery.CRAWLING)); System.out.println(s.equals(Shrubbery.CRAWLING)); System.out.println(s == Shrubbery.CRAWLING); System.out.println(s.getDeclaringClass()); System.out.println("name: " + s.name()); } }
- ordinal(): return an int indicating the declaration order of this enum item, starts from 0.
- we can compare enum using '==', equals() and hashCode() are automatically generated for us.
# Enum
- when we create a new enum type, it will automatically extends Enum class.
- Enum class is Comparable and Serialzable.
# can constructor of enum type be public?
- NO, it should be private or default, public or protected are not allowed.
- complier won't let us to create enum item using constructor.
# how to customize the way of displaying enum item?
- default toString() in Enum class will just return name, we can override toString() to define new format.
# why enum can be used in switch statement?
- Ordinarily, switch statement only accept integral value, BUT enum has ordinal() which will produce the integral order, so complier will do sth like using ordinal() method.
# mystry of values()
- actually, values() and valueOf() are added to enum type by complier.
# can we extends Enum class explicitly? or extends other enum types?
- enum can't extends anything explicitly, because it already extends Enum && java won't allow multi-inheritance
- Also NO for enum types, because complier will make them 'final'.
- BTW, all enum items will be declared as public static final
?? singleton design pattern qeustion??
i've been told that the best practice to write Singleton is using enum, according to the above, the reason maybe all enum items are 'static final', so I'm wondering if we can write Singleton using interface like below:
interface ISingleton { ISingleton instance = new ISingleton() { @Override public void me() { System.out.println("am i singleton??"); } }; void me(); }
# how we get enum instances via Enum class?
- if we upcast our enum type to Enum class, there is no method like values() there,
BUT, we can use getEnumConstants() like below:
Enum e = myEnum.INSTANCE; for (Enum en : e.getClass().getEnumConstants()) { System.out.println(en); }
# enum type can implements one or more interfaces.
# enum of enums
1. interface way
public interface Food { enum Appetizer implements Food { SALAD, SOUP, SPRING_ROLLS; } enum MainCourse implements Food { LASAGNE, BURRITO, PAD_THAI, LENTILS, HUMMOUS, VINDALOO; } enum Dessert implements Food { TIRAMISU, GELATO, BLACK_FOREST_CAKE, FRUIT, CREME_CARAMEL; } enum Coffee implements Food { BLACK_COFFEE, DECAF_COFFEE, ESPRESSO, LATTE, CAPPUCCINO, TEA, HERB_TEA; } }
2. enum way
public enum Course { APPETIZER(Food.Appetizer.class), MAINCOURSE(Food.MainCourse.class), DESSERT(Food.Dessert.class), COFFEE(Food.Coffee.class); private Food[] values; private Course(Class<? extends Food> kind) { values = kind.getEnumConstants(); } public Food randomSelection() { return Enums.random(values); } }
# EnumSet
class EnumSetTest { enum Color { RED, BLUE, BLACK, WHITE } public static void main(String[] args) { EnumSet<Color> eSet = EnumSet.noneOf(Color.class); print(eSet); eSet.addAll(EnumSet.allOf(Color.class)); print(eSet); eSet.removeAll(EnumSet.range(Color.RED, Color.BLACK)); print(eSet); } }
# EnumMap
key is enum
public class EnumMaps { public static void main(String[] args) { EnumMap<AlarmPoints, Command> em = new EnumMap<AlarmPoints, Command>(AlarmPoints.class); em.put(KITCHEN, new Command() { public void action() { print("Kitchen fire!"); } }); em.put(BATHROOM, new Command() { public void action() { print("Bathroom alert!"); } }); for (Map.Entry<AlarmPoints, Command> e : em.entrySet()) { printnb(e.getKey() + ": "); e.getValue().action(); } try { // If there's no value for a particular key: em.get(UTILITY).action(); } catch (Exception e) { print(e); } } }
for some cases, we can put enumSet and enumMap inside the enumType to manage all items.