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, which means that you can refer to an object of an enum type with a reference. An enum type is declared with an enum declaration, which is a comma-separated list of enum constantsthe 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 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 8.10 illustrates how to declare instance variables, a constructor and methods in an enum type. The enum declaration (lines 537) contains two partsthe enum constants and the other members of the enum type. The first part (lines 813) declares six enum constants. Each enum constant is optionally followed by arguments which are passed to the enum constructor (lines 2024). Like the constructors you have seen in classes, an enum constructor can specify any number of parameters and can be overloaded. In this example, the enum constructor has two String parameters, hence each enum constant is followed by parentheses containing two String arguments. The second part (lines 1636) declares the other members of the enum typetwo instance variables (lines 1617), a constructor (lines 2024) and two methods (lines 2730 and 3336).

Figure 8.10. Declaring enum type with instance fields, constructor and methods.

(This item is displayed on pages 377 - 378 in the print version)

 1 // Fig. 8.10: Book.java
 2 // Declaring an enum type with constructor and explicit instance fields
 3 // and accessors for these field
 5 public enum Book
 6 {
 7 // declare constants of enum type 
 8 JHTP6( "Java How to Program 6e", "2005" ), 
 9 CHTP4( "C How to Program 4e", "2004" ), 
10 IW3HTP3( "Internet & World Wide Web How to Program 3e", "2004" ),
11 CPPHTP4( "C++ How to Program 4e", "2003" ), 
12 VBHTP2( "Visual Basic .NET How to Program 2e", "2002" ), 
13 CSHARPHTP( "C# How to Program", "2002" ); 
15 // instance fields
16 private final String title; // book title
17 private final String copyrightYear; // copyright year
19 // enum constructor
20 Book( String bookTitle, String year )
21 {
22 title = bookTitle;
23 copyrightYear = year;
24 } // end enum Book constructor
26 // accessor for field title
27 public String getTitle()
28 {
29 return title;
30 } // end method getTitle
32 // accessor for field copyrightYear
33 public String getCopyrightYear()
34 {
35 return copyrightYear;
36 } // end method getCopyrightYear
37 } // end enum Book

Lines 1617 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 2024) takes two String parameters, one that specifies the book title and one that specifies the copyright year of the book. Lines 2223 assign these parameters to the instance variables. Lines 2736 declare two methods, which return the book title and copyright year, respectively. Figure 8.11 tests the enum type declared in Fig. 8.10 and illustrates how to iterate through a range of enum constants.

Figure 8.11. Testing an enum type.

(This item is displayed on pages 378 - 379 in the print version)

 1 // Fig. 8.11: EnumTest.java
 2 // Testing enum type Book.
 3 import java.util.EnumSet;
 5 public class EnumTest
 6 {
 7 public static void main( String args[] )
 8 {
 9 System.out.println( "All books:
" );
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() );
16 System.out.println( "
Display a range of enum constants:
" );
18 // print first four books
19 for ( Book book : EnumSet.range( Book.JHTP6, Book.CPPHTP4 ) )
20 System.out.printf( "%-10s%-45s%s
", book,
21 book.getTitle(), book.getCopyrightYear() );
22 } // end main
23 } // end class EnumTest
All books:

JHTP6 Java How to Program 6e 2005
CHTP4 C How to Program 4e 2004
IW3HTP3 Internet & World Wide Web How to Program 3e 2004
CPPHTP4 C++ How to Program 4e 2003
VBHTP2 Visual Basic .NET How to Program 2e 2002
CSHARPHTP C# How to Program 2002

Display a range of enum constants:

JHTP6 Java How to Program 6e 2005
CHTP4 C How to Program 4e 2004
IW3HTP3 Internet & World Wide Web How to Program 3e 2004
CPPHTP4 C++ How to Program 4e 2003

For every enum, the compiler generates a static method called values (called in line 12) that returns an array of the enum's constants in the order in which they were declared. Recall from Section 7.6 that the enhanced for statement can be used to iterate through an array. Lines 1214 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. Note that 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., JHTP6 for the first enum constant).

Lines 1921 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 parametersthe first enum constant in the range and the last enum constant in the rangeand returns an EnumSet that contains all the constants between these two constants, inclusive. For example, EnumSet.range( Book.JHTP6, Book.CPPHTP4 ) returns an EnumSet containing Book.JHTP6, Book.CHTP4, Book.IW3HTP3 and Book.CPPHTP4. The enhanced for statement can be used with an EnumSet just as it can with an array, so lines 1921 use the enhanced for statement 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. For more details of class EnumSet, visit java.sun.com/j2se/5.0/docs/api/java/util/EnumSet.html.

Common Programming Error 8.6

In an enum declaration, it is a syntax error to declare enum constants after the enum type's constructors, fields and methods in the enum declaration.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look


Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams


Searching and Sorting

Data Structures



Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2



Accessing Databases with JDBC


JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover

show all menu

Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net