... specify any number of parameters and can be overloaded. In this example, the enum
constructor requires two String
parameters—one that specifies the book’s title and one that specifies its copyright year. To properly initialize each enum
constant, we follow it with parentheses containing two String
arguments.
The second part (lines 15–32) declares the enum’s
other members—instance variables title
and copyrightYear
(lines 15–16), a constructor (lines 19–22) and two methods (lines 25–27 and 30–32) that return the book title and copyright year, respectively. Each constant in type Book
is an object of type Book
that has its own copy of instance variables.
Using enum
type Book
Figure 8.11 tests the Book enum
and illustrates how to iterate through a range ...
Get Java How To Program, Late Objects, 11th 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.