Skip to content

Kotlin basics : Enum in Kotlin

Devrath edited this page Dec 24, 2023 · 1 revision

About ENUM

  • In the application development, we find scenarios where a value can be just one of the collection of values. We name those possibilities, With this, we can make use of the enum class.
  • Basically enum is something of types that has its own possible values.

On a set of values Enum provides certain out of the box features like,

  • Gives a list of all the declared cases in the class. this will help in iterating the list of cases using EnumClass.values().
  • We also get the ordinal property for each of the cases accessed with which we can access the index of the property.
enum class DaysOfTheWeek { MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY }

class DemoEnum {
    init { initiate() }

    private fun initiate() {
        // Iterate the list of possibilities in ENUM class
        iterateValues()
    }

    private fun iterateValues() {
        for (week in DaysOfTheWeek.values()){
            // Ordinal helps in printing the position of the current enum class
            println("Week-Name: $week -- at position -- ${week.ordinal}")
        }
    }
}

  • Like other classes Enum classes can have properties and members also.
  • We can pass a value in the constructor of the Enum, We can later access the value of the each case that is set and use it.
enum class DaysOfTheWeek(val isWeekend: Boolean = false) {
    MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY(isWeekend = true), SUNDAY(isWeekend = true);

    fun isCurrentDayWeekend(daysOfTheWeek : DaysOfTheWeek): Boolean {
        if(this.ordinal<daysOfTheWeek.ordinal){
            return true 
        }else{
            return false
        }
    }
}

class DemoEnum {
    init { initiate() }

    private fun initiate() {
        // Iterate the list of possibilities in ENUM class
        iterateValues()
        isCurrentDayWeekend(DaysOfTheWeek.SUNDAY)
    }

    private fun isCurrentDayWeekend(currentDay: DaysOfTheWeek) {
        if(currentDay.isCurrentDayWeekend(currentDay)){
            println("It is a weekend")
        } else {
            println("It is a weekday")
        }
    }

    private fun iterateValues() {
        for (week in DaysOfTheWeek.values()){
            // Ordinal helps in printing the position of the current enum class
            println("Week-Name: $week -- at position -- ${week.ordinal} -- is weekend ${week.isWeekend}")
        }
    }
}

  • One more possibility is adding the possible causes. We mention one case and we can get auto-completion to add the rest of the existing cases
  • We get a warning if one of the else case is not covered
private fun checkPossibleCases() {
        when(DaysOfTheWeek.THURSDAY) {
            DaysOfTheWeek.MONDAY -> println("Today is Monday")
            DaysOfTheWeek.TUESDAY -> TODO()
            DaysOfTheWeek.WEDNESDAY -> TODO()
            DaysOfTheWeek.THURSDAY -> TODO()
            DaysOfTheWeek.FRIDAY -> TODO()
            DaysOfTheWeek.SATURDAY -> TODO()
            DaysOfTheWeek.SUNDAY -> TODO()
        }
    }
Clone this wiki locally