Declaring Instance Variables, a Constructor and Methods in an enum Type
Figure 8.10 demonstrates instance variables, a constructor and methods in an enum
type. The enum
declaration (lines 5–37) contains two parts—the enum
constants and the other members of the enum
type. The first part (lines 8–13) declares six constants. Each is optionally followed by arguments that are passed to the enum constructor (lines 20–24). Like the constructors you’ve seen in classes, an enum
constructor can specify any number of parameters and can be overloaded. In this example, the enum
constructor requires two String
parameters. To properly initialize each enum
constant, we follow it with parentheses containing two String
arguments. The second part (lines 16–36) ...
Get Java™ How To Program (Early Objects), Tenth 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.