skip to content

enum class & Members

An enum class declares a fixed set of constants that may take constructor parameters, and every constant gets name and ordinal plus a valueOf lookup. Worth knowing: valueOf throws on an unknown name, which is a common parsing bug.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

How do you declare an enum class in Kotlin, and what is an enum constant?

level: juniorimportance: must knowfreq 80%

answer

  1. enum class Name { A, B, C }
  2. each constant = singleton instance
  3. enum is a soft modifier on class
  4. extends kotlin.Enum, can't pick superclass
  5. exhaustive when, no else needed

basics

~10 s

Write 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 s

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

val d: Direction = Direction.NORTH
println(d == Direction.NORTH) // true, same singleton

go deeper

for a junior

Can write the basic enum class { A, B, C } syntax and reference a constant.

for a middle

Knows constants are singletons, the soft-modifier syntax, and when the trailing semicolon is required.

for a senior

Explains the kotlin.Enum supertype, exhaustive when, and the inability to choose a superclass.

for a principal

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

context

open as a page

How do you give each enum constant constructor parameters, and how are those values supplied per constant?

level: middleimportance: must knowfreq 70%

basics

~10 s

Add a constructor to the enum class, then put the argument values in parentheses after each constant, like RED(0xFF0000). Each constant supplies its own values, and you read them as normal properties.

open as a page

What do the built-in `name` and `ordinal` properties of an enum constant return, and what should you be careful about with `ordinal`?

level: middleimportance: must knowfreq 65%

basics

~10 s

name gives the constant's declared identifier as a String (e.g. "RED"). ordinal gives its zero-based position in the declaration list (0, 1, 2...). Be careful: reordering constants changes every ordinal.

open as a page

Why can enum constants be compared safely with `==`, and what does it mean that each constant is a singleton?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Each enum constant exists as exactly one object created once at class load. So referencing RED always gives the same instance, and == (which compares value) and === (which compares identity) both reliably tell constants apart.

open as a page

What does `valueOf()` do for an enum, and how does it behave when given an unknown name?

level: seniorimportance: should knowfreq 55%

basics

~10 s

valueOf("RED") looks up an enum constant by its exact name and returns it. If no constant has that name, it throws an IllegalArgumentException instead of returning null.

open as a page