Chapter 3. Enumerated Types
Creating an Enum
Declaring Enums Inline
Iterating Over Enums
Switching on Enums
Maps of Enums
Sets of Enums
Adding Methods to an Enum
Implementing Interfaces with Enums
Value-Specific Class Bodies
Manually Defining an Enum
Extending an Enum
In Java 1.4 and below, there were two basic ways to define new types:
through classes and interfaces. For most object-oriented programming,
this would seem to be enough. The problem is that there are still some
very specific cases where neither is these is sufficient, most commonly
when you need to define a finite set of allowed values for a specific data
type. For instance, you might want a type called Grade that can only be
assigned values of A, B, C, D, F, or Incomplete. Any other values are illegal
for this type. This sort of construct is possible prior to Tiger, but it
takes a lot of work, and there are still some significant problems.Since we're good developers and try our best to avoid a lot of work
whenever possible, Sun finally helped
us out with the new enumerated
type (generally referred to simply as an enum). This chapter deals with
enums: how to create, use, and program with them.