In Fig. D.5, we introduced the basic enum
type, which defines a set of constants 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
constants are implicitly final
, because they declare constants that shouldn’t be modified.
2. enum
constants are implicitly static
.
3. Any attempt to create an object of an enum type with operator new
results in a compilation error.
The enum
constants can be used anywhere constants can be used, such as in the case
labels of switch
statements and to control enhanced for
statements.
Figure F.10 illustrates how to declare 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 enum
constants. Each is optionally followed by arguments which 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, which are passed to the enum
’s constructor. The second part (lines 16–36) declares the other members of the enum
type—two instance variables (lines 16–17), a constructor (lines 20–24) and two methods (lines 27–30 and 33–36).
1 // Fig. F.10: Book.java
2 // Declaring an enum type with constructor and explicit instance fields
3 // and accessors for these fields
4
5 public enum Book
6 {
7 // declare constants of enum type
8 JHTP( "Java How to Program", "2012"
),
9 CHTP( "C How to Program", "2007"
),
10 IW3HTP( "Internet & World Wide Web How to Program", "2008"
),
11 CPPHTP( "C++ How to Program", "2012"
),
12 VBHTP( "Visual Basic 2010 How to Program", "2011"
),
13 CSHARPHTP( "Visual C# 2010 How to Program", "2011"
);
14
15 // instance fields
16 private final String title; // book title
17 private final String copyrightYear; // copyright year
18
19 // enum constructor
20 Book( String bookTitle, String year )
21 {
22 title = bookTitle;
23 copyrightYear = year;
24 } // end enum Book constructor
25
26 // accessor for field title
27 public String getTitle()
28 {
29 return title;
30 } // end method getTitle
31
32 // accessor for field copyrightYear
33 public String getCopyrightYear()
34 {
35 return copyrightYear;
36 } // end method getCopyrightYear
37 } // end enum Book
Lines 16–17 declare the instance variables title
and copyrightYear
. Each enum
constant in Book
is actually an object of type Book
that has its own copy of instance variables title
and copyrightYear
. The constructor (lines 20–24) takes two String
parameters, one that specifies the book’s title and one that specifies its copyright year. Lines 22–23 assign these parameters to the instance variables. Lines 27–36 declare two methods, which return the book title and copyright year, respectively.
Figure F.11 tests the enum
type Book
and illustrates how to iterate through a range of enum
constants. For every enum
, the compiler generates the static
method values (called in line 12) that returns an array of the enum
’s constants in the order they were declared. Lines 12–14 use the enhanced for
statement to display all the constants declared in the enum Book
. Line 14 invokes the enum Book
’s getTitle
and getCopyrightYear
methods to get the title and copyright year associated with the constant. When an enum
constant is converted to a String
(e.g., book
in line 13), the constant’s identifier is used as the String
representation (e.g., JHTP
for the first enum
constant).
1 // Fig. F.11: EnumTest.java
2 // Testing enum type Book.
3 import java.util.EnumSet;
4
5 public class EnumTest
6 {
7 public static void main( String[] args )
8 {
9 System.out.println( "All books:
" );
10
11 // print all books in enum Book
12 for ( Book book : Book.values() )
13 System.out.printf( "%-10s%-45s%s
", book,
14 book.getTitle(), book.getCopyrightYear() );
15
16 System.out.println( "
Display a range of enum constants:
" );
17
18 // print first four books
19 for ( Book book : EnumSet.range( Book.JHTP, Book.CPPHTP ) )
20 System.out.printf( "%-10s%-45s%s
", book,
21 book.getTitle(), book.getCopyrightYear() );
22 } // end main
23 } // end class EnumTest
All books:
JHTP Java How to Program 2012
CHTP C How to Program 2007
IW3HTP Internet & World Wide Web How to Program 2008
CPPHTP C++ How to Program 2012
VBHTP Visual Basic 2010 How to Program 2011
CSHARPHTP Visual C# 2010 How to Program 2011
Display a range of enum constants:
JHTP Java How to Program 2012
CHTP C How to Program 2007
IW3HTP Internet & World Wide Web How to Program 2008
CPPHTP C++ How to Program 2012
Lines 19–21 use the static
method range of class EnumSet (declared in package java.util
) to display a range of the enum Book
’s constants. Method range
takes two parameters—the first and the last enum
constants in the range—and returns an EnumSet
that contains all the constants between these two constants, inclusive. For example, the expression EnumSet.range( Book.JHTP
, Book.CPPHTP )
returns an EnumSet
containing Book.JHTP
, Book.CHTP
, Book.IW3HTP
and Book.CPPHTP
. The enhanced for
statement can be used with an EnumSet
just as it can with an array, so lines 12–14 use it to display the title and copyright year of every book in the EnumSet
. Class EnumSet
provides several other static
methods for creating sets of enum
constants from the same enum
type.