Class relations: + java.lang.Object + - java.lang.Enum<E>
Parameter type:
E
- The enum type subclass
Implemented interfaces: Serializable, Comparable<E>
Thread-safe: True
Enum or enumeration serves the purpose of representing a collection of named constants (unchangeable variables, similar to final variables). They are used when you know all possible values at compile time, such as choices on a menu, error codes, etc.
The enum is a class type in Java. Enum types are implicitly static and not instantiated using the new
keyword. They have the almost the same capabilities as other classes, you can define constructors, add instance variables, methods and implement interfaces. All enum types you create implicitly extend the java.lang.Enum
class and automatically implement the Comparable interface to achieve the natural ordering among enum constants.
An enum class cannot inherit other class nor can serve as a superclass, because it is already implicitly extended the java.lang.Enum
class (Java does not support multiple inheritance) and is implicitly final (you cannot extend a final class).
Also, enum constants are implicitly public, static, and final. These constants are all instances of the enum type itself. When you define an enum, each constant is a singleton instance of that enum type. An enum type has no instances other than those defined by its enum constants.
Enum has special EnumSet
and EnumMap
for efficient and compact representation of enum constants.
Summary of enum
- Enum is a special class representing a collection of named constants
- Enum class is implicitly final, static, extending
java.lang.Enum
class and implementingComparable
interface- It can have fields, constructors, and methods
- It can implement interfaces
- It cannot inherit other class nor serve as a super class
- Enum constants are implicitly public, static and final
- Enum constants can be traversed and have a natural order
- Each constant is a singleton instance of that enum type
You can study more about enum in Enum 2 - Natural Ordering.
Declaration of enum
To create a enum class, use the enum
keyword. Separate constants in the enum class by comma; you don’t create enum constants using the new
keyword as JVM handles the instantiation during class loading. Like final variables, the constants in enum class are capitalised.
You can create the enum class inside another class (in this case Main
) like a nested class.
public class Main {
public enum Season {
SPRING,
SUMMER,
AUTUMN,
WINTER;
}
}
From this example, the Season
is the enum type, the SPRING
, SUMMER
, AUTUMN
, WINTER
are instances of the Season
enum type.
You can check the type of the constant and validate its singularity like below:
class Main {
public enum Season {
SPRING, SUMMER, AUTUMN, WINTER;
}
public static void main(String[] args) {
Season season1 = Season.AUTUMN;
// check type
System.out.println(season1 instanceof Season);
// validate singularity
Season season2 = Season.AUTUMN;
System.out.println(season1 == season2);
}
}
true
true
Enum constants with fields using constructor
Enum constant can have associated fields using explicitly defined constructor. The fields are for each enum constant, and it is common practice that fields are in final to ensure immutability and thread safety.
Important
An enum constructor is always private. It is a compile-time error if a constructor declaration in an enum declaration is public or protected. When an enum constructor is declared with no access modifiers, it is implicitly private. This is an effective way for enum class to implement the singleton pattern and ensure that enum constants can only be instantiated within the enum itself and not from outside the enum definition.
public enum Status {
SUCCESS(200, "Operation successful"),
ERROR(500, "Server error"),
NOT_FOUND(404, "Resource not found");
private final int code;
private final String description;
// this is a private constructor
Status(int code, String description) {
this.code = code;
this.description = description;
}
public int getCode() {
return code;
}
public String getDescription() {
return description;
}
}
public class Main {
public static void main(String[] args) {
Status status = Status.SUCCESS;
System.out.println(status);
System.out.println(status.getCode());
System.out.println(status.getDescription());
}
}
SUCCESS
200
Operation successful
Enum constants with class body
Enum constants can override methods or have other behaviours
public enum Operation {
ADD {
@Override
public double apply(double x, double y) {
return x + y;
}
},
SUBTRACT {
@Override
public double apply(double x, double y) {
return x - y;
}
},
MULTIPLY {
@Override
public double apply(double x, double y) {
return x * y;
}
},
DIVIDE {
@Override
public double apply(double x, double y) {
return x / y;
}
};
public abstract double apply(double x, double y);
}
Enum constants with nested types
Enum constants that can have nested types such as classes or interfaces, including enum types.
Below is the code with a nested enum type CardColor
.
public enum Suit {
HEARTS(CardColor.RED),
DIAMONDS(CardColor.RED),
CLUBS(CardColor.BLACK),
SPADES(CardColor.BLACK);
// each Suit has an associated enum type CardColor
private final CardColor color;
Suit(CardColor color) {
this.color = color;
}
public CardColor getColor() {
return color;
}
// nested enum class
public enum CardColor {
RED, BLACK;
}
}
Back to parent page: Java Standard Edition (Java SE) and Java Programming
Web_and_App_Development Programming_Languages Java Enum
Reference: