Definition. Enums
[comp1110-2G]
Definition. Enums
[comp1110-2G]An enum (short for enumeration) is a user-definable type whose values come from a fixed finite list. Instead of using arbitrary integers or strings, we define a small set of allowed values and give each one a name. For example:
This defines a new type called Month. A variable of type Month can only hold one of these twelve values. For example:
The names (which are by convention written in ALL_CAPS) inside the enum definition are called constants.
They are always written with the type name in front, such as Month.JANUARY or Month.DECEMBER.
This prevents confusion if different enums contain constants with the same name.