Working with Enumerations
1. Defining Enums
Example: Basic enum
public enum Status { ACTIVE, INACTIVE, PENDING }
Status s = Status.ACTIVE;
| Property | Detail |
| Implicit super | java.lang.Enum |
| Singletons | One instance per constant |
| Constructor | Implicitly private |
2. Adding Fields to Enums
Example: Enum with fields
public enum Planet {
EARTH(5.97e24, 6.37e6),
MARS(6.42e23, 3.39e6);
private final double mass, radius;
Planet(double m, double r) { this.mass = m; this.radius = r; }
public double surfaceGravity() { return 6.674e-11 * mass / (radius * radius); }
}
| Convention | Detail |
| Fields | final recommended |
| Constructor | Called once per constant at class load |
3. Adding Methods to Enums
| Method Type | Detail |
| Instance method | Same body for all constants |
| Constant-specific | Override per constant in { } body |
| Static helper | e.g., fromCode(int c) |
4. Adding Constructors to Enums
| Rule | Detail |
| Access | Implicitly private |
| When called | At class loading, for each constant |
| Cannot be public | Compile error |
5. Using values() Method
| Method | Returns |
values() | Array of all constants in declaration order |
EnumSet.allOf(E.class) | Set view (more efficient) |
6. Using valueOf() Method
| Method | Behavior |
Status.valueOf("ACTIVE") | Returns constant; throws IllegalArgumentException if absent |
| Case-sensitive | Yes — must match exactly |
7. Using ordinal() Method
| Aspect | Detail |
| Returns | Zero-based position in declaration |
| Caution | Don't persist — fragile to reordering |
Warning: Use name() or a custom code field for serialization, not ordinal().
8. Implementing Interfaces in Enums
Example: Enum implements interface
interface Discount { double apply(double price); }
enum Tier implements Discount {
BRONZE { public double apply(double p) { return p * 0.95; } },
GOLD { public double apply(double p) { return p * 0.85; } };
}
| Pattern | Use |
| Strategy | Each constant implements algorithm differently |
| Type token | Enum used as keys in maps/registries |
9. Using EnumSet Collections
Example: EnumSet
EnumSet<Day> weekend = EnumSet.of(Day.SATURDAY, Day.SUNDAY);
EnumSet<Day> workdays = EnumSet.complementOf(weekend);
EnumSet<Day> all = EnumSet.allOf(Day.class);
| Method | Use |
of(...) | Specific constants |
noneOf(E.class) | Empty set |
range(from, to) | Inclusive range |
| Implementation | Bit vector — extremely fast |
10. Using EnumMap Collections
| Aspect | Detail |
| Construction | new EnumMap<>(Day.class) |
| Storage | Array indexed by ordinal |
| Performance | Faster than HashMap for enum keys |
| Iteration order | Natural enum order |
11. Comparing Enums
| Comparison | Method |
| Equality | == safe (singletons) or equals() |
| Order | compareTo() uses ordinal |
| In switch | Use unqualified constant names |
12. Understanding Enum Singleton Pattern
Example: Enum singleton
public enum Config {
INSTANCE;
private final Properties props = loadProps();
public String get(String key) { return props.getProperty(key); }
}
Config.INSTANCE.get("env");
| Benefit | Detail |
| Thread-safe init | Class loader guarantees |
| Serialization-safe | JVM ensures single instance |
| Reflection-safe | Cannot instantiate via reflection |