How do you declare an enum class in Kotlin, and what is an enum constant?
answer
- enum class Name { A, B, C }
- each constant = singleton instance
- enum is a soft modifier on class
- extends kotlin.Enum, can't pick superclass
- exhaustive when, no else needed
basics
~10 sWrite enum class Name { A, B, C }. Each capitalized name inside the braces (A, B, C) is a constant — a fixed, named instance of that enum that you reference as Name.A.
solid answer
~40 sAn enum class declares a fixed set of named instances. You write `enum class Color { RED, GREEN, BLUE }`; RED, GREEN, BLUE are the enum constants — each is a singleton object of type Color. The keyword pair is `enum class` (enum is a soft modifier on class). Constants are listed first in the body, comma-separated. You reference them by qualified name (`Color.RED`) and they can be used in exhaustive `when` expressions. Each constant is created exactly once at class load. Enums implicitly extend `kotlin.Enum`, so they inherit `name`, `ordinal`, `compareTo`, `equals`/`hashCode`, and a synthetic `valueOf`/`entries`. If the body has only constants, the trailing semicolon after the list is optional.
code
kotlin · 4 linesenum class Direction { NORTH, EAST, SOUTH, WEST }
val d: Direction = Direction.NORTH
println(d == Direction.NORTH) // true, same singletongo deeper
Can write the basic enum class { A, B, C } syntax and reference a constant.
Knows constants are singletons, the soft-modifier syntax, and when the trailing semicolon is required.
Explains the kotlin.Enum supertype, exhaustive when, and the inability to choose a superclass.
Discusses tradeoffs of enums vs sealed classes for closed sets and the implications for binary/API evolution.
## What an enum class is An **enum class** is a class whose set of instances is **fixed and known at compile time**. You declare it with the keyword pair `enum class`. Each named value you list is an **enum constant** — a single, pre-created instance of the class. ```kotlin enum class Direction { NORTH, EAST, SOUTH, WEST } ``` Here `Direction` is the type and `NORTH`, `EAST`, `SOUTH`, `WEST` are its four constants. Each is a **singleton**: `Direction.NORTH` always refers to the very same object, created once when the class is loaded. ## Key facts - **Syntax**: `enum` is a *soft modifier* applied to `class`. You cannot write just `enum Foo` — it must be `enum class Foo`. - **Listing constants**: constants come **first** in the class body, comma-separated. If the enum has *nothing else* in the body, the trailing semicolon is optional. If you add members (properties/functions) after the constants, you must terminate the constant list with a `;`. - **Type**: every constant has the enum type. `Direction.NORTH` is a `Direction`. - **Inheritance**: every Kotlin enum implicitly extends the abstract class `kotlin.Enum<E>`. You **cannot** specify a superclass (enums already have one), but they *can* implement interfaces (a sibling topic). - **Usage in `when`**: because the instance set is closed, a `when` over an enum can be **exhaustive** — if you cover every constant, no `else` branch is required. ```kotlin fun describe(d: Direction): String = when (d) { Direction.NORTH -> "up" Direction.EAST -> "right" Direction.SOUTH -> "down" Direction.WEST -> "left" } ``` ## Referencing constants You use the qualified name `Direction.NORTH`. Inside a `when` you can import them or rely on smart context. Constants compare with `==` (identity-safe because they are singletons) and also work with `===`. ## What you get for free Because of the `kotlin.Enum` supertype, each constant automatically has a `name` (its declared identifier as a String) and an `ordinal` (its zero-based position), plus the companion-like `valueOf(name)` and `entries` accessors. Those are covered in detail in other questions.
- Can an enum class extend another class?No. Every enum implicitly extends kotlin.Enum, so you cannot declare a superclass. It can, however, implement one or more interfaces.
- When is the semicolon after the constant list required?Only when the body contains members (properties/functions) after the constants. With constants alone the trailing semicolon is optional.
Like the compass points or days of the week — a small, closed, named set that never grows at runtime.
saying these in an interview costs you the question
- Writing `enum Foo` without the `class` keyword
- Thinking each constant is a new instance every time it's referenced
- Claiming enums can extend an arbitrary base class
- Saying a when over an enum always needs an else branch
- Confusing enum constants with companion-object members