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

How do you declare and use an enumerated type in Java? | ComputerScienceOne | Bifalgorithm | Bifalgorithm