> ## 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.

# Kotlin Enum Class

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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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`).

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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 (`;`).

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
interface Describable {
    fun describe(): String
}

enum class Role : Describable {
    ADMIN {
        override fun describe() = "Full system access"
    },
    USER {
        override fun describe() = "Standard access"
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
