0Pricing
Kotlin Multiplatform Academy · Lesson

Enums & Sealed Classes for State

Model fixed options and result variants safely.

Enums & Sealed Classes for State is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Model Fixed Choices

Some values have a known, limited set of options. Kotlin gives you enums and sealed classes to model them safely in shared code.

Meet the enum class

An enum class lists every allowed value by name. Nothing outside that list can ever sneak in.

enum class Status { ACTIVE, PAUSED, DONE }

Use an enum Value

Refer to an option by its name. Because the set is closed, your state is always one of the listed cases.

val current = Status.ACTIVE

Enums Can Carry Data

Give an enum constructor properties so each case can hold extra fixed info, like a label or numeric code.

enum class Plan(val price: Int) { FREE(0), PRO(9) }

When Cases Need Different Data

Enums share one shape. When variants need different fields, reach for a sealed class instead.

Meet the sealed class

A sealed class defines a closed family of subtypes. Each subtype can carry its own distinct data.

sealed class Result
data class Ok(val value: String) : Result()
data class Err(val message: String) : Result()

Why Sealed Is Powerful

Because the compiler knows every subtype, a sealed class models success, loading and error states with type-safe payloads.

val state: Result = Ok("loaded")

Match with when

Handle each case using when. The compiler checks you covered every variant of the sealed type.

when (state) {
  is Ok -> println(state.value)
  is Err -> println(state.message)
}

Exhaustive when, No else

When you handle all sealed cases, you can drop the else branch. Add a new subtype later and the compiler reminds you.

Shared, So Both Apps Agree

Defined in commonMain, these state types drive identical logic on Android and iOS with zero drift.

Pick the Right Tool

Use an enum for simple fixed options, and a sealed class when each case carries different data.

Quick Check

Which type should model these states?

Recap

Use shared enums for fixed options and sealed classes for variants with their own data, then match them safely with when.

Frequently asked questions

Is the “Enums & Sealed Classes for State” lesson free?

Yes — the full text of “Enums & Sealed Classes for State” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Enums & Sealed Classes for State”?

Model fixed options and result variants safely. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Enums & Sealed Classes for State” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Data Classes for Your Domain
  2. Enums & Sealed Classes for State
  3. Null Safety in Shared Models
  4. Validation Helpers on Your Models
← Back to Kotlin Multiplatform Academy