Enumerations, Autoboxing,
and Annotations (Metadata)
his chapter examines three recent additions to the Java language: enumerations,
autoboxing, and annotations (also referred to as metadata). Each expands the power
of the language by offering a streamlined approach to handling common programming
tasks. This chapter also discusses Java's type wrappers and introduces reflection.
Versions prior to JDK 5 lacked one feature that many programmers felt was needed:
enumerations. In its simplest form, an enumeration is a list of named constants. Although
Java offered other features that provide somewhat similar functionality, such as final
variables, many programmers still missed the conceptual purity of enumerations--especially
because enumerations are supported by most other commonly used languages. Beginning
with JDK 5, enumerations were added to the Java language, and they are now available to
the Java programmer.
In their simplest form, Java enumerations appear similar to enumerations in other
languages. However, this similarity is only skin deep. In languages such as C++, enumerations
are simply lists of named integer constants. In Java, an enumeration defines a class type. By
making enumerations into classes, the concept of the enumeration is greatly expanded. For
example, in Java, an enumeration can have constructors, methods, and instance variables.
Therefore, although enumerations were several years in the making, Java's rich
implementation made them well worth the wait.
Enumeration Fundamentals
An enumeration is created using the enum keyword. For example, here is a simple
enumeration that lists various apple varieties:
// An enumeration of apple varieties.
enum Apple {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks