Java Reference
In-Depth Information
Display 6.13
An Enumerated Type (part 2 of 2)
8 System.out.println("Work starts on " + startDay);
9 System.out.println("Work ends on " + endDay);
10 }
11 }
Sample Dialogue
Work starts on MONDAY
Work ends on FRIDAY
Enumerated Type
An enumerated type is a type for which you give all the values of the type in a typically short
list. A value of an enumerated type is a kind of named constant and so, by convention, is
spelled with all uppercase letters.
SYNTAX
enum Type_Name { FIRST_VALUE , SECOND_VALUE , ... , LAST_VALUE };
Starting with version 5.0, enum is a keyword in Java.
EXAMPLE
enum Flavor {VANILLA, CHOCOLATE, STRAWBERRY};
The definition of an enumerated type is normally placed outside of all methods in the same
place that you give named constants. The location for an enumerated type definition is illus-
trated in Display 6.13. (The definition can be placed in other locations, but we will not need to
place them anywhere else.)
You can output the value of a variable of an enumerated type using println . For
example,
System.out.println(WorkDay.THURSDAY);
will produce the following screen output:
THURSDAY
Note that the type name WorkDay is not output. Other examples of outputting an enu-
merated type value are given in Display 6.13.
 
Search WWH ::




Custom Search