Instruction

Throughout this lesson, you’ll learn:

  • When to use enum classes in your project.
  • How to define enum classes.
  • How to use enum classes in code.

How Do You Declare Enum Class?

Enum classes provide a way to represent a fixed set of constants within a type-safe structure. They help you define a collection of related constants distinct from each other. You can declare an enum class using the enum class keyword followed by the name of the class:

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

Here, you declare an enum class called DayOfWeek and initialize it with the days of the week.

Here’s another example where you create an enum called ErrorType that represents different types of errors.

enum class ErrorType {
  NOT_FOUND, INVALID_INPUT, SERVER_ERROR
}

Now that you understand how enums work, it’s time to explore them in more depth with more complete examples.

enum class DropdownOption {
  OPTION_BANANA, OPTION_APPLE, OPTION_GRAPE
}
fun main() {
  val currentSelection = DropdownOption.OPTION_BANANA
  println("Current selection: $currentSelection")
}

Here’s a code breakdown:

  1. You declare an enum class named DropdownOption. This enum class represents different options in a dropdown menu. It includes three constants: OPTION_BANANA, OPTION_APPLE, and OPTION_GRAPE.

  2. In the main() function, you create a variable named currentSelection and assign it the value DropdownOption.OPTION_BANANA

Reason of Using Enum Class

There are a few reasons to use enum classes.

Type Safety Enum classes provide type safety by ensuring only valid values from the defined set can be used. This helps prevent bugs and runtime errors caused by incorrect or invalid values.

Here is an example:

fun createFruitBox(fruitBox: String): FruitBox {
  return when (fruitBox) {
    "Banana" -> Banana()
    "Apple" -> Apple()
    "Grape" -> Grape()
    else -> throw IllegalArgumentException("Invalid fruit")
  }
}

Looking at the code, you can see the fruitBox is a String type. The issue here is that passing any string value may lead to errors or unexpected behavior. Now, take a look at another example where you use an enum class instead of a string:

enum class Fruits {
  BANANA,
  APPLE,
  GRAPE
}

fun createFruitBox(fruitBox: Fruits): FruitBox {
  return when (fruitBox) {
    Fruits.BANANA -> Banana()
    Fruits.APPLE -> Apple()
    Fruits.GRAPE -> Grape()
    else -> throw IllegalArgumentException("Invalid fruit")
  }
}

Based on the code above, you’ll see that the fruitBox is a Fruits enum class type. This approach ensures that only specific values can be passed, and the compiler will catch any attempt to use an invalid value that isn’t part of the Fruits enum class.

Improved Code Maintainability By arranging all possible values in a single enum class, you make modifying or extending the set of values easier in the future. This also helps with reading the code and better maintaining it for future use.

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo