Comprehensive Guide to Java Enums: Constants, Switch Statements, Custom Methods, Overriding, Interfaces, and Collections
This article explains how Java enums introduced in JDK 1.5 can be used as grouped constants, in switch statements, to hold fields and methods, to override behavior, to implement interfaces, and to work with EnumSet and EnumMap collections, providing clear code examples for each use case.
Usage 1: Constants
Before JDK 1.5 constants were defined with public static final; enums now allow related constants to be grouped in a single type, offering additional built‑in methods.
public enum Color { RED, GREEN, BLANK, YELLOW }Usage 2: Switch
Enums can be used in switch statements, improving readability compared to primitive types.
enum Signal { GREEN, YELLOW, RED }
public class TrafficLight {
Signal color = Signal.RED;
public void change() {
switch (color) {
case RED: color = Signal.GREEN; break;
case YELLOW: color = Signal.RED; break;
case GREEN: color = Signal.YELLOW; break;
}
}
}Usage 3: Adding Custom Methods
Enums can contain fields, constructors, and regular methods; a semicolon must separate the enum constants from the rest of the definition.
public enum Color {
RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
private String name;
private int index;
private Color(String name, int index) { this.name = name; this.index = index; }
public static String getName(int index) {
for (Color c : Color.values()) {
if (c.getIndex() == index) return c.name;
}
return null;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getIndex() { return index; }
public void setIndex(int index) { this.index = index; }
}Usage 4: Overriding Methods
Enums can override methods such as toString() to provide custom string representations.
public enum Color {
RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
private String name;
private int index;
private Color(String name, int index) { this.name = name; this.index = index; }
@Override
public String toString() {
return this.index + "_" + this.name;
}
}Usage 5: Implementing Interfaces
All enums extend java.lang.Enum; they can also implement interfaces to add behavior.
public interface Behaviour {
void print();
String getInfo();
}
public enum Color implements Behaviour {
RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
private String name;
private int index;
private Color(String name, int index) { this.name = name; this.index = index; }
@Override
public String getInfo() { return this.name; }
@Override
public void print() { System.out.println(this.index + ":" + this.name); }
}Usage 6: Organizing Enums with Interfaces
An interface can serve as a common type for multiple enums.
public interface Food {
enum Coffee implements Food { BLACK_COFFEE, DECAF_COFFEE, LATTE, CAPPUCCINO }
enum Dessert implements Food { FRUIT, CAKE, GELATO }
}Usage 7: Enum Collections
Java provides EnumSet (a high‑performance Set) and EnumMap (a Map with enum keys) for efficient handling of enum collections.
For deeper implementation details, refer to the fourth edition of "Thinking in Java".
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
