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.
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:
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.
SUCCESS
200
Operation successful
Enum constants with class body
Enum constants can override methods or have other behaviours
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
.
Back to parent page: Java Standard Edition (Java SE) and Java Programming
Web_and_App_DevelopmentProgramming_LanguagesJavaEnum
Reference: