skip to content

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

level: middleimportance: must knowfreq 70%

answer

  1. primary constructor on the enum class
  2. each constant calls it: EARTH(5.97e24, ...)
  3. use val params -> per-constant properties
  4. semicolon required after constant list if body follows
  5. constructed once, in declaration order

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.

solid answer

~40 s

Declare a primary constructor on the enum class with `val` parameters, then each constant calls that constructor with its own arguments: `enum class Planet(val mass: Double) { EARTH(5.97e24), MARS(6.42e23) }`. Each constant initializes its properties once at class load. The constructor can have multiple parameters, default values, and the properties are immutable if declared `val` (recommended). After the constant list you must add a `;` before declaring the properties/functions that follow — though the `val mass` here is in the constructor, not the body, so no semicolon is needed unless you add body members. Constants are constructed in declaration order. You access values via `Planet.EARTH.mass`. The constructor is effectively private to the enum — you can't call it externally to make new constants.

code

kotlin · 11 lines
kotlin
enum class Coin(val cents: Int) {
    PENNY(1),
    NICKEL(5),
    DIME(10),
    QUARTER(25);

    fun dollars(): Double = cents / 100.0
}

println(Coin.QUARTER.cents)     // 25
println(Coin.DIME.dollars())    // 0.1

go deeper

for a junior

Can add a constructor and pass a value per constant, then read it as a property.

for a middle

Knows the mandatory semicolon rule, default/named arguments, and val-vs-var implications.

for a senior

Explains construction order, once-only initialization, and why val keeps singletons safe.

for a principal

Weighs enums-with-data vs sealed classes/data objects for richer per-instance state and evolution.

## Per-constant constructor arguments An enum class can declare a **primary constructor**, and then **each constant supplies its own constructor arguments** in parentheses. This lets every constant carry associated data. ```kotlin enum class Planet(val massKg: Double, val radiusM: Double) { MERCURY(3.30e23, 2.44e6), EARTH(5.97e24, 6.37e6), JUPITER(1.90e27, 7.15e7); val surfaceGravity: Double get() = 6.67e-11 * massKg / (radiusM * radiusM) } ``` ### How it works - The **primary constructor** `(val massKg: Double, val radiusM: Double)` is declared right after the enum name, exactly like any class. - Each **constant** then *calls* that constructor: `EARTH(5.97e24, 6.37e6)`. The values in parentheses are the constructor arguments for **that constant only**. - Declaring parameters as **`val`** turns them into read-only properties on every constant, so `Planet.EARTH.massKg` works. ## The mandatory semicolon If the enum body contains **anything after the constant list** (here the `surfaceGravity` property), you **must** terminate the constant list with a **`;`**. Notice the `;` after `JUPITER(...)` above. Forgetting it is a compile error. ## Defaults and multiple parameters The constructor supports everything a normal constructor does: ```kotlin enum class HttpStatus(val code: Int, val retriable: Boolean = false) { OK(200), TOO_MANY_REQUESTS(429, retriable = true), SERVER_ERROR(500, true) } ``` - `OK(200)` uses the default `retriable = false`. - You can pass named arguments (`retriable = true`) just like any function call. ## Construction semantics - Each constant is constructed **exactly once**, at class initialization, **in declaration order**. - The enum constructor is implicitly only callable internally; external code **cannot** instantiate new constants — the set is closed. - Prefer **`val`** for the parameters so the per-constant data is immutable. A `var` is legal but allows mutating shared singleton state, which is usually a design smell. ## Accessing the data ```kotlin val g = Planet.EARTH.surfaceGravity val status = HttpStatus.TOO_MANY_REQUESTS println(status.code) // 429 println(status.retriable) // true ```

  • Do you need a semicolon after the constant list here?
    Yes — whenever the enum body declares anything after the constants (a property or function), the constant list must end with `;`.
  • Can two constants share the same constructor argument value?
    Yes. They remain distinct constants (distinct singletons with distinct names/ordinals) even if their stored data is identical.
  • Should constructor parameters be val or var?
    Prefer val. Constants are shared singletons, so a var would let code mutate global state and cause hard-to-trace bugs.

saying these in an interview costs you the question

  • Forgetting the `;` after the constant list when body members follow
  • Trying to call the enum constructor from outside to create new constants
  • Using `var` for per-constant data without realizing it's shared mutable singleton state
  • Thinking constructor arguments belong to the class once, not per constant
  • Believing two constants with equal data are the 'same' constant

context