Java Reference
In-Depth Information
* name.
* The string must match exactly an identifier used to declare
* an enum constant in this type. (Extraneous whitespace
* characters are not permitted.)
*
* @return the enum constant with the specified name
* @throws IllegalArgumentException if this enum type has no
* constant with the specified name
*/
public static E valueOf(String name);
It follows that enum type declarations cannot contain fields that conflict with the
enum constants, and cannot contain methods that conflict with the automatically gen-
erated methods ( values() and valueOf(String) ) or methods that override the final methods
in Enum ( equals(Object) , hashCode() , clone() , compareTo(Object) , name() , ordinal() , and getDe-
claringClass() ).
It is a compile-time error to reference a static field of an enum type that is not a constant
variable (§ 4.12.4 ) from constructors, instance initializer blocks, or instance variable initial-
izer expressions of that type.
It is a compile-time error for the constructors, instance initializer blocks, or instance vari-
able initializer expressions of an enum constant e to refer to e or to an enum constant of the
same type that is declared to the right of e .
Example 8.9.2-1. Restriction On Enum Constant Self-Reference
Without this rule, apparently reasonable code would fail at run time due to the ini-
tialization circularity inherent in enum types. (A circularity exists in any class with a
“self-typed” static field.) Here is an example of the sort of code that would fail:
Click here to view code image
import java.util.Map;
import java.util.HashMap;
enum Color {
RED, GREEN, BLUE;
static final Map<String,Color> colorMap =
new HashMap<String,Color>();
Color() { colorMap.put(toString(), this); }
}
Search WWH ::




Custom Search