8.9. Enumerations

In Fig. 6.9 (Craps.java), we introduced the basic enum type which defines a set of constants that are represented as unique identifiers. In that program, the enum constants represented the game’s status. In this section, we discuss the relationship between enum types and classes. Like classes, all enum types are reference types. An enum type is declared with an enum declaration, which is a comma-separated list of enum constants—the declaration may optionally include other components of traditional classes, such as constructors, fields and methods. Each enum declaration declares an enum class with the following restrictions:

  1. enum types are implicitly final, because they declare constants that should not be modified.

  2. enum constants ...

Get Java™ How to Program, Seventh 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.