Understanding and Using Java Enums: Definitions, Basic and Advanced Techniques
This article introduces Java enums, explains their definition, basic usage such as declaring, accessing, and iterating values, demonstrates advanced features like adding fields, implementing interfaces, using them in switch statements, and shows how enums can implement design patterns such as Singleton and Strategy.
1. What is a Java Enum?
Java enum is a special class type that defines a fixed set of constant values, and can also contain methods and constructors, providing a type‑safe way to represent a limited set of predefined values.
2. Basic Usage of Java Enums
1. Defining an enum type
Example of a simple enum representing the seven days of the week:
public enum DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}2. Accessing enum values
You can access enum constants directly via the enum type name:
DayOfWeek today = DayOfWeek.WEDNESDAY;
System.out.println("Today is " + today);3. Iterating over enum values
Use a for‑each loop to iterate:
for (DayOfWeek day : DayOfWeek.values()) {
System.out.println(day);
}3. Advanced Usage of Java Enums
1. Adding fields and methods
Enums can have fields and methods to provide additional information:
public enum Color {
RED("红色"),
GREEN("绿色"),
BLUE("蓝色");
private String chineseName;
Color(String chineseName) {
this.chineseName = chineseName;
}
public String getChineseName() {
return chineseName;
}
}2. Implementing interfaces
Enums can implement interfaces, allowing each constant to provide its own implementation:
interface Printable {
void print();
}
public enum Shape implements Printable {
CIRCLE {
@Override
public void print() {
System.out.println("这是一个圆形。");
}
},
SQUARE {
@Override
public void print() {
System.out.println("这是一个正方形。");
}
};
}3. Using enums in switch statements
Enums can be used in switch cases for clearer code:
DayOfWeek day = DayOfWeek.FRIDAY;
switch (day) {
case MONDAY:
System.out.println("周一,新的一周开始了。");
break;
case FRIDAY:
System.out.println("周五,周末即将到来。");
break;
// other cases
}4. Implementing Design Patterns with Enums
1. Singleton pattern
Enum provides a simple way to implement a singleton:
public enum PizzaDeliverySystemConfiguration {
INSTANCE;
// fields and methods
}2. Strategy pattern
Enum can define strategy implementations:
public enum PizzaDeliveryStrategy {
EXPRESS {
@Override
public void deliver(Pizza pz) {
System.out.println("Pizza will be delivered in express mode");
}
},
NORMAL {
@Override
public void deliver(Pizza pz) {
System.out.println("Pizza will be delivered in normal mode");
}
};
public abstract void deliver(Pizza pz);
}5. Conclusion
Java enums offer a powerful and flexible way to handle a fixed set of constants, making code clearer, safer, and more maintainable when used appropriately.
Top Architecture Tech Stack
Sharing Java and Python tech insights, with occasional practical development tool tips.
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.