The “enum” keyword in Java declares a specialized type defining a fixed set of constants.
|
Enum constants can be accessed using the dot notation.
Level myVar = Level.MEDIUM; |
The “enum” keyword defines an enumerated (immutable) type.
An enum is a distinctive “class” that embodies a collection of constants (immutable variables, akin to final variables).
To construct an enum, utilize the “enum” keyword (instead of “class” or “interface”) and delineate the constants with commas. Ensure they are in uppercase letters.
An enum, akin to a class, can possess attributes and methods. The distinction lies in the fact that enum constants are public, static, and final (immutable – incapable of being overridden). Enums are ineligible for object instantiation, and they are incapable of extending other classes (although they can implement interfaces). Utilize enums for values that are anticipated to remain constant, such as month names, days of the week, colors, a deck of cards, etc. |