Java, 一杯浓浓的咖啡伴你到深夜

《Java 5.0 Tiger》Chapter 3

Chapter 3. Enumerated Types

3.1 Creating an Enum

More often than not, you'll only need the basic enum functionality:

public enum Grade { A, B, C, D, F, INCOMPLETE };

The convention is to use all capital letters for enumerated type identifiers.

"Grade" is used just like anyother Java type:

public class Student {
private String firstName;

private String lastName;

private Grade grade;

public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;

public void assignGrade(Grade grade) {
this.grade = grade;

public Grade getGrade() {
return grade;


Pretty basic, isn't it? The final piece is actually using this code in conjunction with the enum, as shown here:

    public void testGradeAssignment(PrintStream out) throws IOException {
        Student student1 
= new Student("Brett""McLaughlin");
        Student student2 
= new Student("Ben""Rochester");
        Student student3 
= new Student("Dennis""Erwin");

Enums are classes
Enums extend java.lang.Enum
Enumerated types aren't integers
Enums have no public constructor
Enum values are public, static, and final
Enum values can be compared with == or equals( )
Enums implements java.lang.Comparable
Enums override toString( )
Enums provide a valueOf( ) method
Enums define a final instance method named ordinal( )
Enums define a values( ) method

3.2 Declaring Enums Inline

public class Downloader {
public enum DownloadStatus {
// Class body

Nested enums are implicitly static.

3.3 Iterating Over Enums

Invoking the values( ) method on an enum returns an array of all the values in the type:

    public void listGradeValues(PrintStream out) throws IOException {
        Grade[] gradeValues 
= Grade.values();
for (Grade g : Grade.values()) {
"Allowed value: '" + g + "'");


3.4 Switching on Enums

Prior to Java 1.4, switch only worked with int, short, char, and byte values. However, since enums have a finite set of values, Tiger adds switch support for them:

    public void testSwitchStatement(PrintStream out) throws IOException {
        StringBuffer outputText 
= new StringBuffer(student1.getFullName());
switch (student1.getGrade()) {
case A:
" excelled with a grade of A");
case B: // fall through to C
        case C:
" passed with a grade of ")
case D: // fall through to F
        case F:
" failed with a grade of ")
" did not complete the class.");
" has a grade of ")


Tiger simply requires that you not preface each enumerated type with the enum class name. In fact, it's a compilation error if you do!

3.5 Maps of Enums

You'll need to use the java.util.EnumMap class to accomplish this, which is a new collection type just perfect for the job. First, you need to define the enum you want to use for a keyset:

    public enum AntStatus {

You can now create a new EnumMap, and pass the EnumMap the Class object for the enum used for the keyset:

    public void testEnumMap(PrintStream out) throws IOException {
// Create a map with the key and a String message
        EnumMap<AntStatus, String> antMessages = new EnumMap<AntStatus, String>(
// Initialize the map
        antMessages.put(AntStatus.INITIALIZING, "Initializing Ant");
"Compiling Java classes");
"Copying files");
"JARring up files");
"ZIPping up files");
"Build complete.");
"Error occurred.");
// Iterate and print messages
        for (AntStatus status : AntStatus.values()) {
"For status " + status + ", message is: "
+ antMessages.get(status));


3.6 Sets of Enums

Similar to Maps of Enums, but use java.util.EnumSet. Here are the methods of this class you should be concerned with, most of which are factories:

       // Returns a new EnumSet with all elements from the supplied type
       public static EnumSet allOf(Class elementType);

// Returns a new EnumSet of the same type as the supplied set, but
// with all the values not in the supplied set; a mirror image
       public static EnumSet complementOf(EnumSet e);

// Returns a new EnumSet from the provided collection
       public static EnumSet copyOf(Collection c);

// Returns a new EnumSet with no values in it
       public static EnumSet noneOf(Class elementType);

// Various methods to create an EnumSet with the supplied elements in it
       public static EnumSet of(E e[, E e2, E e3, E e4, E e5]);

// Varags version
       public static EnumSet of(E e);

// Creates an EnumSet with a range of values
       public static EnumSet range(E from, E to);

// returns a copy of the current set - not a factory method
       public EnumSet clone( );

3.7 Adding Methods to an Enum

    public enum GuitarFeatures {
0), // back/sides
        MAHOGANY(0), // back/sides
        ZIRICOTE(300), // back/sides
        SPRUCE(0), // top
        CEDAR(0), // top
        AB_ROSETTE(75), // abalone rosette
        AB_TOP_BORDER(400), // abalone top border
        IL_DIAMONDS(150), // diamond/square inlay
        IL_DOTS(0); // dots inlays

/** The upcharge for the feature */
private float upcharge;

float upcharge) {
this.upcharge = upcharge;

public float getUpcharge() {
return upcharge;


public String getDescription() {
switch (this{
return "Rosewood back and sides";
return "Mahogany back and sides";
return "Ziricote back and sides";
case SPRUCE:
return "Sitka Spruce top";
case CEDAR:
return "Wester Red Cedar top";
return "Abalone rosette";
return "Abalone top border";
return "Diamonds and squares fretboard inlay";
case IL_DOTS:
return "Small dots fretboard inlay";
return "Unknown feature";


There are quite a few things here that you'll need to take note of. First, the class now has a constructor that takes in a float parameter for the upcharge of each feature. As a result, each enumerated type now passes in a parameter to the constructor.

Then, variables are declared, and methods appear, just like any other class.

You cannot put your variable declarations before the enumerated values. All declarations must follow the enumerated type declarations.

...limiting access to the enum constructor? Enum constructors are implicitly private, so this is taken care of for you.

3.8 Implemening Interfaces with Enums

public class Downloader {
public interface Features {
/** Get the upcharge for this feature */
public float getUpcharge();

/** Get the description for this feature */
public String getDescription();


It's trivial to make GuitarFeatures implement this interface, as the methods are already written:

public enum GuitarFeatures implements Features {

3.9 Value-Specific Class Bodies

// These are the opcodes that our stack machine can execute.
enum Opcode {
// Push the single operand onto the stack
public void perform(StackMachine machine, int[] operands) {

// Remember to separate enum values with commas

// Add the top two values on the stack and put the result
public void perform(StackMachine machine, int[] operands) {
+ machine.pop());


/* Other opcode values have been omitted for brevity */

// Branch if Equal to Zero
public void perform(StackMachine machine, int[] operands) {
if (machine.pop() == 0)

// Remember the required semicolon after last enum value

// This is the constructor for the type.
    Opcode(int numOperands) {
this.numOperands = numOperands;

int numOperands; // how many integer operands does it expect?

// Each opcode constant must implement this abstract method in a
// value-specific class body to perform the operation it represents.
    public abstract void perform(StackMachine machine, int[] operands);

...just using a more generic method that determines what to do based on a switch statement? Well, that's a better idea, to be honest. Here's the (much cleaner) way to write OpCode:

// These are the the opcodes that our stack machine can execute.
abstract static enum Opcode {
1); // Remember the required semicolon after last enum value

int numOperands;

int numOperands) {
this.numOperands = numOperands;

public void perform(StackMachine machine, int[] operands) {
switch (this{
case PUSH:
case ADD:
+ machine.pop());
case BEZ:
if (machine.pop() == 0)
throw new AssertionError();



3.10 Manually Defining an Enum

You couldn't menually define your own enum—at least, not in Tiger. While this is very much an accessible class, and is indeed the base class of all enumerated types in Tiger, the compiler won't let you extend it, as following:

// Attempting to compile this class give you error
public class ExtendedEnum extends Enum {


3.11 Extending an Enum

Here's another one of those pesky, "You don't" labs. Tiger does not allow extension of an enum. For example:

// Attempting to compile this class give you error
public enum CollegeGrade extends Grade { DROP_PASSING, DROP_FAILING }

In theory, this would take the values Grade.A, Grade.B, and so forth, and add to them two new values, CollegeGrade.DROP_PASSING and CollegeGrade.DROP_FAILING. However, you'll get compilation errors if you try this.

posted on 2005-12-29 23:28 Scott@JAVA 阅读(461) 评论(0)  编辑  收藏 所属分类: Java 5.0 Tiger