0Pricing
Kotlin Academy · Lesson

Defining Enums

Named constant sets.

Defining Enums is a free Kotlin Academy lesson on CoddyKit — lesson 1 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 Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is an Enum?

An enum class defines a fixed set of named constants. Use it when a value can only be one of a small, known group of options.

  • Each constant is an instance of the enum type.
  • The compiler guarantees no other values are possible.
enum class Direction {
    NORTH, SOUTH, EAST, WEST
}

fun main() {
    val d = Direction.NORTH
    println(d)
}

Why Not Just Use Strings?

Strings allow typos and invalid values. An enum makes invalid states impossible: the compiler rejects anything outside the declared set.

enum class Status { ACTIVE, INACTIVE }

fun main() {
    val s: Status = Status.ACTIVE
    println("Current: $s")
}

Declaring Constants

Constants are listed between the braces, separated by commas. By convention they are written in UPPER_SNAKE_CASE.

enum class Size {
    SMALL, MEDIUM, LARGE, EXTRA_LARGE
}

fun main() {
    println(Size.EXTRA_LARGE)
}

Each Constant Is an Object

Every enum constant is a singleton instance of the enum type. You always reference it through the enum name, like Color.RED.

enum class Color { RED, GREEN, BLUE }

fun main() {
    val c = Color.GREEN
    println(c == Color.GREEN)
}

Comparing Enum Values

Enum constants can be compared with ==. Because each is a singleton, equality and reference identity match.

enum class Light { RED, YELLOW, GREEN }

fun main() {
    val signal = Light.RED
    if (signal == Light.RED) println("Stop")
}

Reading the name Property

Every constant exposes a built-in name property: the exact string of its declaration.

enum class Planet { MERCURY, VENUS, EARTH }

fun main() {
    val p = Planet.EARTH
    println(p.name)
}

valueOf From a String

The generated valueOf() function converts a matching string into the enum constant. A non-matching string throws an exception.

enum class Day { MON, TUE, WED }

fun main() {
    val d = Day.valueOf("TUE")
    println(d)
}

Using Enums as Parameters

Enums make great function parameters: callers can only pass one of the valid constants.

enum class Level { LOW, HIGH }

fun setVolume(level: Level) {
    println("Volume set to $level")
}

fun main() {
    setVolume(Level.HIGH)
}

Enums Improve Readability

Code that reads Direction.NORTH is self-documenting. Compared to magic numbers like 0 or 1, intent is obvious.

enum class Gear { PARK, DRIVE, REVERSE }

fun main() {
    val current = Gear.DRIVE
    println("Gear is $current")
}

A Realistic Example

Modeling a small set of card suits is a perfect fit for an enum.

enum class Suit { HEARTS, DIAMONDS, CLUBS, SPADES }

fun main() {
    for (suit in Suit.values()) {
        println(suit)
    }
}

Single-Constant Enums

Even one constant is valid, often used to model a guaranteed-single value (a poor man(rsquo)s singleton).

enum class AppMode { PRODUCTION }

fun main() {
    println(AppMode.PRODUCTION)
}

Quick Check

Test your understanding of defining enums.

Recap

You learned to define enums:

  • enum class declares a fixed set of named constants.
  • Each constant is a singleton instance accessed via the enum name.
  • name and valueOf() bridge between constants and strings.
  • Enums prevent invalid values at compile time.
enum class Direction { NORTH, SOUTH, EAST, WEST }

fun main() {
    println(Direction.valueOf("EAST").name)
}

Frequently asked questions

Is the “Defining Enums” lesson free?

Yes — the full text of “Defining Enums” is free to read here on the web, and the Kotlin 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 Academy course, upgrade to CoddyKit PRO.

What will I learn in “Defining Enums”?

Named constant sets. You practise Kotlin 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 Academy?

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

How long does the “Defining Enums” 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 Academy lesson?

Yes. Every Kotlin 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. Defining Enums
  2. Enum Properties and Methods
  3. Enums with when
  4. Enum Values and Ordinals
← Back to Kotlin Academy