|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | CONST | FIELD | CONSTR | METHOD | DETAIL: CONST | FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--satin.common.Enum
public abstract class Enum
extends java.lang.ObjectAn abstract base class for "enum" classes. This is a base class for deriving classes that behave something like C or C++ "enum" types, except that they don't have integer values.
public enum NewEnglandState
{
VERMONT, MASSACHUSETTS, CONNECTICUT, NEW_HAMPSHIRE, MAINE, RHODE_ISLAND
};
This would define the symbol NewEnglandState to be
a new user-defined type, whose set of values are the symbols
enclosed in curly braces (VERMONT, ... RHODE_ISLAND).
When used outside of their definition, values would have to be explicitly
scoped to their type (for example NewEnglandState.VERMONT).
The only operations allowable would be assignment and a test for
equality, for example:
NewEnglandState s; // declare variable s to be of type NewEnglandState
// pretend s gets initialized somewhere
if (s == NewEnglandState.NEW_HAMPSHIRE) // comparison
s = NewEnglandState.VERMONT; // assignment
Enum.
For each value of the enum, we statically instantiate one object of
that derived class, and assign it to a constant class variable
declared within the derived class.
Additional run-time instantiations are prevented by making the
the constructor is protected.
Thus, the complete set of objects of the derived enum class that can
ever exist are statically defined within the class.
This is better than using integer contants because (a) it prevents, at compile time, using invalid integer values, and (b) it provides better self-documenting code.
We would define a class to represent a state as follows:
public class NEstate extends Enum
{
protected NEstate (String s) { super(s); }
public static final NEstate VERMONT = new NEstate("VERMONT");
public static final NEstate MASSACHUSETTS = new NEstate("MASSACHUSETTS");
public static final NEstate CONNECTICUT = new NEstate("CONNECTICUT");
public static final NEstate NEW_HAMPSHIRE = new NEstate("NEW_HAMPSHIRE");
public static final NEstate MAINE = new NEstate("MAINE");
public static final NEstate RHODE_ISLAND = new NEstate("RHODE_ISLAND");
}
We could then use variables of type NEstate like this:
NEstate s = NEstate.VERMONT;
// ...
if (s == NEstate.MAINE)
System.out.println("ayuh");
Note that the following code will not compile:
NEstate s = new NEstate("Quebec");
Because the constructor is protected, objects of class NEstate
can only be instantiated within the class itself. This means that
there can be no additional (bogus) states created at runtime.
| Field Summary | |
final java.lang.String |
name
The name of the enum value. |
| Constructor Summary | |
protected |
Enum(java.lang.String name)
Constructs an enum value. |
| Method Summary | |
java.lang.String |
toString()
Returns a string representation of the enum value. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
public final java.lang.String name
| Constructor Detail |
protected Enum(java.lang.String name)
namename field.| Method Detail |
public java.lang.String toString()
toString in class java.lang.Object
| Revision History |
5/31/01 Ed Stauff - made changes to docs per initial code review.
3/26/01 Ed Stauff - improved example of usage.
3/21/01 Ed Stauff - initial version reviewed by automation team.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | CONST | FIELD | CONSTR | METHOD | DETAIL: CONST | FIELD | CONSTR | METHOD | ||||||||
Enum.java (PRIVATE SOURCES)