Enum Classes in Kotlin

Last Updated : 1 Sep, 2026

In programming, some variables can have only a fixed set of predefined values, such as days of the week, directions, or card suits. Kotlin provides enum classes to represent these values in a structured and type-safe way.

  • Enum classes represent a predefined set of constants.
  • Each enum entry is an instance of the enum class.
  • Enum classes can include properties, functions, and constructors.

Enum in Kotlin

An enum, short for enumeration, is a special class that represents a fixed set of constants. Each enum entry is an instance of the enum class.

For example:

Kotlin
enum class Day {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
}

In this example, Day is an enum class with seven entries, one for each day of the week.

Adding Properties to Enum Values

In Kotlin, enum classes can have a primary constructor and properties. Each enum entry is an instance of the enum class and can pass specific values to the primary constructor.

Example to specifying colors for cards:

Kotlin
enum class Cards(val color: String) {
    Diamond("black"),
    Heart("red")
}


Here, each card has a color. You can access the color like this:

val color = Cards.Diamond.color
println(color)

Output:

black

Enum Properties and Methods

Kotlin enum classes provide built-in properties and functions that can be used to access information about enum entries and retrieve enum constants.

Properties

  • ordinal: Returns the zero-based position of an enum entry.
  • name: Returns the name of an enum entry.

Functions and Properties for Accessing Entries

  • entries: Returns all entries defined in the enum class. This is the modern Kotlin approach.
  • values(): Returns an array containing all enum constants. It is still supported for compatibility.
  • valueOf(): Returns the enum constant whose name matches the specified string. It throws an IllegalArgumentException if no matching constant exists.

Example to demonstrate enum class in Kotlin

Kotlin
enum class DAYS {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY
}

fun main() {
    for (day in DAYS.entries) {
        println("${day.ordinal} = ${day.name}")
    }

    println(DAYS.valueOf("WEDNESDAY"))
}

Output:

0 = SUNDAY
1 = MONDAY
2 = TUESDAY
3 = WEDNESDAY
4 = THURSDAY
5 = FRIDAY
6 = SATURDAY
WEDNESDAY

Enum Class Properties and Functions

An enum class in Kotlin defines a new type and can have its own properties and functions, just like a regular class. Properties can be initialized through the enum class constructor, and functions can define behavior for individual enum entries. A companion object can also be used for functions or properties that belong to the enum class itself rather than a specific enum entry.

Example to demonstrate properties and functions in Kotlin

Kotlin
// A property with a default value
enum class DAYS(val isWeekend: Boolean = false) {
    SUNDAY(true),
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY(true);

    companion object {
        fun isWeekend(day: DAYS): Boolean {
            return day == SATURDAY || day == SUNDAY
        }
    }
}

fun main() {
    for (day in DAYS.entries) {
        println("${day.ordinal} = ${day.name} and is weekend ${day.isWeekend}")
    }

    val today = DAYS.MONDAY
    println("Is today a weekend ${DAYS.isWeekend(today)}")
}

Output:

0 = SUNDAY and is weekend true
1 = MONDAY and is weekend false
2 = TUESDAY and is weekend false
3 = WEDNESDAY and is weekend false
4 = THURSDAY and is weekend false
5 = FRIDAY and is weekend false
6 = SATURDAY and is weekend true
Is today a weekend false

Enums as Anonymous Classes

In Kotlin, enum entries can behave like anonymous classes by providing their own implementation of functions. When an enum class declares an abstract function, each enum entry must override and implement that function.

Kotlin
enum class Weather {
    SUMMER {
        override fun description() = "Hot days of the year"
    },
    WINTER {
        override fun description() = "Cold days of the year"
    };

    abstract fun description(): String
}


The function can be called as follows:

println(Weather.SUMMER.description())

Output:

Hot days of the year

Usage of when Expression with an Enum Class

Enum classes work effectively with Kotlin's when expression. Since an enum class has a fixed set of entries, an else branch is not required when all enum entries are handled.

If an enum entry is omitted from a when expression that must be exhaustive, the compiler reports that the expression is not exhaustive.

Example:

Kotlin
enum class DAYS{
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY;
}
 
fun checkDay(day: DAYS) {
    when (day) {
        DAYS.SUNDAY -> println("Today is Sunday")
        DAYS.MONDAY -> println("Today is Monday")
        DAYS.TUESDAY -> println("Today is Tuesday")
        DAYS.WEDNESDAY -> println("Today is Wednesday")
        DAYS.THURSDAY -> println("Today is Thursday")
        DAYS.FRIDAY -> println("Today is Friday")
        DAYS.SATURDAY -> println("Today is Saturday")
    }
}

Since all enum entries are handled, an else branch is not required.

Comment

Explore