Enum is a special class type which defines an ordered enumeration of names, each associated with an integer value. Enum inherits Object, but cannot be instantiated. Its use is strictly limited to immutable access to its members in a static context.
To declare an enum,enum Color {
RED = 0,
YELLOW = 1,
GREEN = 2
}
The integer value defined for each name is not required. If not provided, it's defaulted to the value of previous name plus 1. In the case of first name, it's defaulted to 0. Each value must be distinct within the enum, otherwise would cause definition exception when the type is loaded.
The name of an enum can be referred to by normal addressing syntax, such asColor c = Color.YELLOW;
However, these names cannot be used as a left value.
An enum variable, when defined without an initializer, will be initialized to the first name of that enum type. The null value cannot be assigned to an enum variable.
An enum's member has two constant fields. literal exposes the name as a string, ordinal the associated integer value.
Julian provides syntax-level support for switch logic based on an enum variable:Color c = ...;
switch(c){
case RED: ...
case YELLOW: ...
case GREEN: ...
}
For more detailed description on Enum, see tutorial on Enum.
Parent Class
Type | Name | Signature |
---|---|---|
field C | ordinal | public const int ordinal |
field C | literal | public const String literal |
public const int ordinal
The integer value associated with this member.
public const String literal
The name of this member, exactly as is defined in the type's definition.