Concept
How do you declare and use an enumerated type in Java?
ComputerScienceOne / More Tricks
"In the example, since the name of the enumeration is Day this declaration must be in a source file named Day.java. We can now declare variables of this type. The possible values it can take are restricted to SUNDAY, MONDAY, etc. and we can use these keywords in our program. However these values belong to the class Day and must be accessed statically. For example,\n\nDay today = Day.MONDAY;\n\nif(today == Day.SUNDAY || today == Day.SATURDAY) {\n\nSystem.out.println(\"Its the weekend!\");\n\n}\n\nNote the naming conventions: the name of the enumerated type follows the same upper camel casing that all classes use while the enumerated values follow an uppercase underscore convention. Though our example does not contain a value with multiple words, if it had, we would have used an underscore to separate them."
Related Ideas
- What built-in methods and customization options do Java enum types provide?ComputerScienceOne · More Tricks
- How do you declare an enumerated type in C?ComputerScienceOne · Enumerated Types
- Why are enumerated types better than arbitrary integer error codes?ComputerScienceOne · Enumerated Types
- How do enumerated types represent named values in C?ComputerScienceOne · Using Enumerated Types for Error Codes
- How can enumerated types be used for error codes in C?ComputerScienceOne · Using Enumerated Types for Error Codes