|
| | Before Java 5.0 (a.k.a. 1.5), in order to represent an enumerated set
of values (like an enum in C or C++), you would have to do something like the following:
package examples;
public class EnumTest
{
public static final int SMALL = 0,
MEDIUM = 1,
LARGE = 2,
XLARGE = 3;
public static void main(String[] args)
{
int size = LARGE;
System.out.println("Size: " + size);
// Nothing stops you from doing the following:
size = 75;
}
}
|
which outputs:
Size: 2
This is neither:
- convenient: We have to introduce irrelevant integer values
for each of the enumerated values, and manually ensure that they are
distinct. A real pain!
nor
- safe: The code in red above shows that you can use a value
not in the enumerated set, and the compiler will not check it for
validity. This is a problem waiting to happen!
|