Use cases of Java enumeration
JDK 1.5 introduces a new type -- enumeration. In Java, it's just a small feature, but it can bring us much convenience.Here we summarize some use cases of Java enumeration.
Prior to JDK 1.5, we can define constant as public static final..., now we can use enumeration to group all constants in one enum variable and it also provides some useful functions.
Prior to JDK 1.6, only int,char and enum are supported in switch statement, with enum, we can write more readable codes.
3. Add new methods in enum
If we want to define our own methods, then we have to add a semicolon after the implementation of enum variable.
4. Override methods in enum
Here is an example of overriding toString() method.
5. Implement interface
All enum objects are inherited from java.lang.Enum class. Since Java doesn't support multiple inheritance, so enum cannot inherit other classes.
6. Use interface to organize enumerations
7. About use of enum collection
java.util.EnumSet and java.util.EnumMap are two enumeration collections. EnumSet will guarantee the element in the set is unique. key in EnumMap are enum type, and value can be any data type.
A mathematician was interviewing for a job. The interviewer asks him - "You are walking towards your office and running late for a very important meeting and you glimpse a building on fire with people screaming for help. What will you do?".The mathematician thinks for a while and replies : "People's