Open In App

Enum Classes in Kotlin

Last Updated : 08 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In programming, sometimes we want a variable to have only a few specific values. For example, days of the week or card suits (like Heart, Spade, etc.). To make this possible, most programming languages support something called enumeration or enum.

Enums are a list of named constants. Kotlin supports enums in a very powerful way, and unlike some other languages like java, Kotlin enums are actually classes. This means they can have properties, methods, and even constructors.

What is an Enum in Kotlin?

An enum (short for enumeration) is a special type in Kotlin that holds a fixed set of constant values. Each value is like an object, and you cannot create new instances of them.

For example:

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


In this example, DAYS is an enum class with 7 values, one for each day of the week.

Adding Properties to Enum Values

In Kotlin also enums can have a constructor like Java enums. Since enum constants are instances of an Enum class, the constants can be initialized by passing specific values to the primary constructor.

Here is an example of 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

Similar to Java and in other programming languages, Kotlin enum classes has some inbuilt properties and functions which can be used by the programmer. Here's a look at the major properties and methods.

Properties -

  1. ordinal: This property stores the ordinal value of the constant, which is usually a zero-based index.
  2. name: This property stores the name of the constant.

Methods -

  1. values: This method returns a list of all the constants defined within the enum class.
  2. valueOf: This methods returns the enum constant defined in enum, matching the input string. If the constant, is not present in the enum, then an IllegalArgumentException is thrown.

Example to demonstrate enum class in Kotlin

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

public class Main {
    public static void main(String[] args) {
        // A simple demonstration of properties and methods
        for (DAYS day : DAYS.values()) {
            System.out.println(day.ordinal() + " = " + day.name());
        }
        System.out.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

Since enum class in Kotlin, defines a new type. This class type can have its own properties and functions. The properties can be given a default value, however, if not provided, then each constant should define its own value for the property. In the case of functions, they are usually defined within the companion objects so that they do not depend on specific instances of the class. However, they can be defined without companion objects also.

Example to demonstrate properties and functions in Kotlin

Kotlin
// A property with default value provided
enum class DAYS(val isWeekend: Boolean = false){
    SUNDAY(true),
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    // Default value overridden
    SATURDAY(true);
 
    companion object{
        fun today(obj: DAYS): Boolean {
            return obj.name.compareTo("SATURDAY") == 0 || obj.name.compareTo("SUNDAY") == 0
        }
    }
}
 
fun main(){
    // A simple demonstration of properties and methods
    for(day in DAYS.values()) {
        println("${day.ordinal} = ${day.name} and is weekend ${day.isWeekend}")
    }
    val today = DAYS.MONDAY;
    println("Is today a weekend ${DAYS.today(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

Enum constants also behaves as anonymous classes by implementing their own functions along with overriding the abstract functions of the class. The most important thing is that each enum constant must be override.

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

    abstract fun description(): String
}


This can called as

println(Weather.SUMMER.description())

Output:

Hot days of a year

Usage of when expression with enum class

A great advantage of enum classes in Kotlin comes into play when they are combined with the when expression. The advantage is since enum classes restrict the value a type can take, so when used with the when expression and the definition for all the constants are provided, then the need of the else clause is completely eliminated. In fact, doing so will generate a warning from the compiler.

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")
    }
}

If you forget a day, the compiler will warn you.


Next Article
Article Tags :

Similar Reads