Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
enum class Direction {
    NORTH, SOUTH, EAST, WEST
}

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.
val direction = Direction.EAST

println(direction.name)    // Output: EAST
println(direction.ordinal) // Output: 2

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).
val allDirections = Direction.entries
val parsedDirection = Direction.valueOf("NORTH")

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.
import kotlin.enums.enumEntries

inline fun <reified T : Enum<T>> printEnumInfo(name: String) {
    val entries = enumEntries<T>()
    println("Enum has ${entries.size} entries.")
    
    val specificConstant = enumValueOf<T>(name)
    println("Found: ${specificConstant.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.
enum class HttpError(val code: Int, val message: String) {
    UNAUTHORIZED(401, "Unauthorized Access"),
    NOT_FOUND(404, "Resource Not Found"),
    TIMEOUT(408, "Request Timeout")
}

// Accessing properties
val error = HttpError.NOT_FOUND
println(error.code) // Output: 404

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 (;).
enum class ProtocolState {
    WAITING {
        override fun signal(): ProtocolState = TALKING
    },
    TALKING {
        override fun signal(): ProtocolState = WAITING
    }; // Semicolon is mandatory here

    abstract fun signal(): ProtocolState
    
    fun reset(): ProtocolState = WAITING
}

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.
interface Describable {
    fun describe(): String
}

enum class Role : Describable {
    ADMIN {
        override fun describe() = "Full system access"
    },
    USER {
        override fun describe() = "Standard access"
    }
}
Master Kotlin with Deep Grasping Methodology!Learn More