Java Reference
In-Depth Information
Use autoboxing with methods
Understand how autoboxing works with expressions
Apply static import
Gain an overview of annotations
T his chapter discusses enumerations, autoboxing, static import, and annotations. Al-
though none of these were part of the original definition of Java, each having been added by
JDK 5, they significantly enhanced the power and usability of the language. In the case of
enumerations and autoboxing, both addressed what was, at the time, long-standing needs.
Static import streamlined the use of static members. Annotations expanded the kinds of in-
formation that can be embedded within a source file. Collectively, these features offered a
better way to solve common programming problems. Frankly, today, it is difficult to ima-
gine Java without them. They have become that important. Also discussed in this chapter
are Java's type wrappers.
Enumerations
In its simplest form, an enumeration is a list of named constants that define a new data
type. An object of an enumeration type can hold only the values that are defined by the
list. Thus, an enumeration gives you a way to precisely define a new type of data that has a
fixed number of valid values.
Enumerations are common in everyday life. For example, an enumeration of the coins
used in the United States is penny, nickel, dime, quarter, half-dollar, and dollar. An enu-
meration of the months in the year consists of the names January through December. An
enumeration of the days of the week is Sunday, Monday, Tuesday, Wednesday, Thursday,
Friday, and Saturday.
From a programming perspective, enumerations are useful whenever you need to define
a set of values that represent a collection of items. For example, you might use an enumer-
ation to represent a set of status codes, such as success, waiting, failed, and retrying, which
indicate the progress of some action. In the past, such values were defined as final vari-
ables, but enumerations offer a more structured approach.
Enumeration Fundamentals
An enumeration is created using the enum keyword. For example, here is a simple enu-
meration that lists various forms of transportation:
Search WWH ::




Custom Search