enum Keyword in Java
The enum
keyword in Java is used to define a set of named constants. It is a special data type that enables variable to be a set of predefined constants, making the code more readable and maintainable. Enums are commonly used to represent a collection of related constants, such as days of the week, months of the year, or states in a process.
Usage
Enums provide a way to define a collection of constants in a type-safe manner. They can include methods and fields, and they can implement interfaces.
Syntax
enum EnumName {
CONSTANT1, CONSTANT2, CONSTANT3;
}
EnumName
: The name of the enum.CONSTANT1, CONSTANT2, CONSTANT3
: The constants defined within the enum.
Examples
Example 1: Basic Enum
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
public class EnumExample {
public static void main(String[] args) {
Day today = Day.MONDAY;
System.out.println("Today is: " + today);
}
}
In this example, an enum Day
is defined with constants representing the days of the week. The variable today
is assigned the value Day.MONDAY
, and the program prints the current day.
Example 2: Enum with Methods
enum Size {
SMALL, MEDIUM, LARGE;
public String getSize() {
switch (this) {
case SMALL: return "Small Size";
case MEDIUM: return "Medium Size";
case LARGE: return "Large Size";
default: return "Unknown Size";
}
}
}
public class EnumWithMethodExample {
public static void main(String[] args) {
Size size = Size.MEDIUM;
System.out.println("Selected size: " + size.getSize());
}
}
This example demonstrates an enum Size
with a method getSize()
that returns a string representation of the size. The variable size
is assigned Size.MEDIUM
, and the program prints the corresponding size description.
Example 3: Enum with Fields and Constructor
enum Planet {
MERCURY(3.303e+23, 2.4397e6),
VENUS(4.869e+24, 6.0518e6),
EARTH(5.976e+24, 6.37814e6);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double getMass() {
return mass;
}
public double getRadius() {
return radius;
}
}
public class EnumWithFieldsExample {
public static void main(String[] args) {
for (Planet planet : Planet.values()) {
System.out.printf("Planet: %s, Mass: %f, Radius: %f%n", planet, planet.getMass(), planet.getRadius());
}
}
}
In this example, the Planet
enum has fields mass
and radius
, a constructor to initialize these fields, and getter methods to access them. The program iterates through all the enum constants and prints their details.
Tips and Best Practices
- Use for Constants: Use enums to define a fixed set of constants, improving code readability and maintainability.
- Type Safety: Enums provide type safety compared to traditional constant definitions using
final static
. - Switch Statements: Enums work seamlessly with switch statements, making the code cleaner.
switch (size) { case SMALL: // Handle small size break; case MEDIUM: // Handle medium size break; case LARGE: // Handle large size break; }
- Implement Interfaces: Enums can implement interfaces, allowing for more flexible and reusable code.
interface Describable { String getDescription(); } enum Color implements Describable { RED, GREEN, BLUE; @Override public String getDescription() { return "Color: " + this.name(); } }
- Avoid Overuse: While enums are powerful, avoid overusing them for simple constants. Use them when there is a clear set of related constants.