An enum is static by default. There is an Enum class too, read about the differences here.
The simplest use case
public enum Status {
TODO, DOING, DONE
}
Properties and more
The enum values can have properties assigned. The type may contain other methods that calculate using the values. It may also contain a main method.Note: If the values have properties, these conditions must be fulfilled:
- Semicolon after the last value declaration
- A field for each property
- Required arguments constructor
public enum Status {
TODO(0, "A"),
DOING(1, "B"),
DONE(2, "C");
private final int code;
private final String variant;
private Status(int code, String variant) {
this.code = code;
this.variant = variant;
}
public int getCode() {
return code;
}
public String getVariant() {
return variant;
}
}
EnumMap
It is recommended to use EnumMap if the key is enum. The syntax to create one is:
Map<Status, Integer> map = new EnumMap<Status, Integer>(Status.class);
Then it can be used like any map with put and get.
No comments:
Post a Comment