Mastering Enums in Kotlin: A Guide to Simplify Your Code

D'haval Asodariya
Towards Dev
Published in
3 min readMar 21, 2023

--

Kotlin is a modern, powerful programming language that offers developers many features and tools to write efficient and readable code. One of these features is Enums, which are a data type that allow you to define a set of named constants. In this article, we’ll explore advanced use cases of Kotlin enums with code examples, to help you better understand how to use them in your own projects.

Enum Classes

Enum classes in Kotlin are a powerful way to represent a set of constants in your code. They allow you to define a type with a finite set of values, and you can use these values throughout your code.

For example, let’s define an enum class that represents the days of the week:

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

Now we can use this enum throughout our code, for example:

fun getWeekendDays(): List<DayOfWeek> {
return listOf(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY)
}

Enum Properties:

Enum classes in Kotlin can also have properties. This allows you to add additional information to each constant in the enum. For example, let’s add a property to our DayOfWeek enum that represents the number of the day in the week:

enum class DayOfWeek(val number: Int) {
MONDAY(1),
TUESDAY(2),
WEDNESDAY(3),
THURSDAY(4),
FRIDAY(5),
SATURDAY(6),
SUNDAY(7)
}

Now we can use this property throughout our code, for example:

fun getDayNumber(day: DayOfWeek): Int {
return day.number
}

Enum Methods:

Enum classes in Kotlin can also have methods. This allows you to add additional functionality to each constant in the enum.

For example, let’s add a method to our DayOfWeek enum that returns whether the day is a weekday or a weekend day:

enum class DayOfWeek(val number: Int) {
MONDAY(1),
TUESDAY(2),
WEDNESDAY(3),
THURSDAY(4),
FRIDAY(5),
SATURDAY(6),
SUNDAY(7);

fun isWeekend(): Boolean {
return this == SATURDAY || this == SUNDAY
}
}

Now we can use this method throughout our code, for example:

fun printDayType(day: DayOfWeek) {
if (day.isWeekend()) {
println("$day is a weekend day.")
} else {
println("$day is a weekday.")
}
}

Enum Constructors:

Enum classes in Kotlin can also have constructors. This allows you to add additional information to each constant in the enum at the time of creation.

For example, let’s add a constructor to our DayOfWeek enum that takes a display name for each day:

enum class DayOfWeek(val number: Int, val displayName: String) {
MONDAY(1, "Monday"),
TUESDAY(2, "Tuesday"),
WEDNESDAY(3, "Wednesday"),
THURSDAY(4, "Thursday"),
FRIDAY(5, "Friday"),
SATURDAY(6, "Saturday"),
SUNDAY(7, "Sunday");

override fun toString(): String {
return displayName
}
}

Now we can use the display name of each day throughout our code, for example:

fun printDayName(day: DayOfWeek) { 
println("The day of the week is ${day.displayName}")
}

Enum Extensions:

Enum classes in Kotlin can also be extended just like regular classes. This allows you to add additional functionality to the enum that can be used throughout your code.

For example, let’s extend our DayOfWeek enum with a method that returns the next day of the week:

fun DayOfWeek.nextDay(): DayOfWeek {
return when (this) {
MONDAY -> TUESDAY
TUESDAY -> WEDNESDAY
WEDNESDAY -> THURSDAY
THURSDAY -> FRIDAY
FRIDAY -> SATURDAY
SATURDAY -> SUNDAY
SUNDAY -> MONDAY
}
}

Now we can use this extension method throughout our code, for example:

fun printNextDay(day: DayOfWeek) {
println("The next day is ${day.nextDay()}")
}

Conclusion:

Kotlin enums are a powerful feature that allow you to define a set of named constants in your code. They can be used to simplify your code, make it more readable, and add additional functionality to your code. By using the advanced use cases of Kotlin enums, you can create more robust and efficient code that is easier to maintain and understand.

--

--

SDE-III at DhiWise 🤓 | Kotlin Expert 💻 | Android Enthusiastic 📱