0Pricing
Kotlin Academy · Lesson

Enum Values and Ordinals

Iterate and index.

Enum Values and Ordinals is a free Kotlin Academy lesson on CoddyKit — lesson 4 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.

Listing All Constants

The generated values() function returns an array of every constant in declaration order.

enum class Suit { HEARTS, CLUBS }

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

entries Instead of values()

Modern Kotlin prefers entries, which returns an immutable list and avoids creating a new array each call.

enum class Suit { HEARTS, CLUBS }

fun main() {
    for (s in Suit.entries) println(s)
}

The ordinal Property

Every constant has an ordinal: its zero-based position in the declaration order.

enum class Size { S, M, L }

fun main() {
    println(Size.S.ordinal)
    println(Size.L.ordinal)
}

Counting Constants

Use entries.size to count how many constants exist.

enum class Day { MON, TUE, WED }

fun main() {
    println(Day.entries.size)
}

Iterating with Index

Combine ordinals or indices to number each constant.

enum class Color { RED, GREEN, BLUE }

fun main() {
    for (c in Color.entries) {
        println("${c.ordinal}: $c")
    }
}

valueOf and name Recap

Pair valueOf() (string to constant) with name (constant to string) for round-trip conversion.

enum class Status { ON, OFF }

fun main() {
    val s = Status.valueOf("ON")
    println(s.name)
}

Mapping Over Constants

Because entries is a list, you can use collection functions like map.

enum class Coin(val cents: Int) { PENNY(1), DIME(10) }

fun main() {
    val names = Coin.entries.map { it.name }
    println(names)
}

Finding by Property

Search constants with first or find based on a property.

enum class Coin(val cents: Int) { PENNY(1), DIME(10) }

fun main() {
    val c = Coin.entries.first { it.cents == 10 }
    println(c)
}

Ordinals Are Fragile

Avoid storing ordinal values long-term. Reordering constants changes ordinals, breaking saved data. Store name instead.

enum class Level { LOW, HIGH }

fun main() {
    println(Level.HIGH.name)
}

Next Constant by Ordinal

You can navigate constants using ordinals and the entries list.

enum class Gear { FIRST, SECOND, THIRD }

fun next(g: Gear): Gear {
    val all = Gear.entries
    return all[(g.ordinal + 1) % all.size]
}

fun main() {
    println(next(Gear.FIRST))
}

Putting It Together

Summarize an enum by iterating and reading both name and ordinal.

enum class Planet { MERCURY, VENUS, EARTH }

fun main() {
    Planet.entries.forEach {
        println("${it.ordinal} -> ${it.name}")
    }
}

Quick Check

Test your understanding of enum values and ordinals.

Recap

You learned to enumerate and index constants:

  • entries (preferred) and values() list all constants.
  • ordinal is the zero-based declaration position.
  • entries is a list, so collection functions work.
  • Prefer storing name over ordinal for durability.
enum class Size { S, M, L }

fun main() {
    println(Size.entries.size)
    println(Size.M.ordinal)
}

Frequently asked questions

Is the “Enum Values and Ordinals” lesson free?

Yes — the full text of “Enum Values and Ordinals” 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 “Enum Values and Ordinals”?

Iterate and index. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Enum Values and Ordinals” 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