Skip to main content
An enum (enumerated) class in Kotlin is a specialized class used to model types that represent a finite, distinct set of constant values. Each constant defined within an enum class is an object instance of that enum type, making them singletons by nature. Because they are full-fledged classes, Kotlin enums can hold state, implement interfaces, and define custom behavior.

Instance Properties

The enum class itself implicitly inherits from the kotlin.Enum base class, and the constants are instances of that class. This base class provides standard properties for introspection on the individual instances:
  • name: A String representing the exact identifier of the enum constant as declared in the code.
  • ordinal: An Int representing the zero-based position of the constant in the declaration order.

Synthetic Class Members

To facilitate constant retrieval, the Kotlin compiler generates synthetic members directly on the enum class itself. These act similarly to static members and are accessed via the class name, rather than through individual constant instances:
  • entries: A pre-allocated EnumEntries<E> list containing all constants in declaration order. Introduced in Kotlin 1.9, this is a memory-efficient replacement for the legacy values() array.
  • valueOf(value: String): Returns the enum constant matching the specified string. Throws an IllegalArgumentException if no match exists.
  • values(): Returns an Array<E> of all enum constants (legacy approach, superseded by entries).

Generic Access

Kotlin provides standard library functions to access enum constants dynamically when the specific enum type is represented by a reified generic type parameter T. This is critical for generic programming where the exact enum class is not known at compile time:
  • enumEntries<T>(): Returns the EnumEntries<T> list for the given enum type. Note that this requires an explicit import from kotlin.enums.
  • enumValues<T>(): Returns an Array<T> of the enum constants.
  • enumValueOf<T>(name: String): Returns the enum constant of type T with the specified name.

Constructors and Properties

Enum classes can declare a primary constructor to encapsulate state. Because each enum constant is an instance of the class, it must provide the required arguments to the primary constructor at the time of declaration.

Custom Methods and Anonymous Classes

Enum classes can declare member functions. Furthermore, an enum class can declare abstract functions, forcing each constant to provide its own implementation via an anonymous class. When defining member functions inside an enum class, the list of enum constants must be terminated with a semicolon (;).

Interface Implementation

Enum classes cannot inherit from other classes (as they already implicitly inherit from kotlin.Enum), but they can implement one or more interfaces. The interface methods can be implemented globally by the enum class itself, or overridden individually by each constant’s anonymous class.
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More