7.13 Enumeration Types

Enumeration types are designed to increase the readability of programs. The enumeration type enum is a special kind of class declaration. It allows us to define a set of named constant objects that can be used instead of numbers in a program.

Enum types are useful for managing ordered sets where each member of the set has a name. Examples are the days of the week, months of the year, and playing cards. To represent these sets in a program, we often use numbers, such as 1 through 7 for days of the week or 1 through 12 for months of the year. The problem is that to input or output these values, we need to convert between our internal numeric representation (for example, 1–7) and the words that users recognize (Sunday, ...

Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.