Kotlin Enum Class Tutorial With Example

Like other language, Kotlin enum is a special type used to define collection of constants.

In this tutorial, we will learn about what is enum class, syntax to use it, how to initialize it, how enum class is different from regular class etc.

Syntax to define enum class is –

enum class<enum_class_name> {
   constant1, constant2, constant3
}

In this case, an enum class with name enum_class_name will defined that contains three constants contant1, constant2 and constant3. Note that each constant is separated by comma. Similarly, you can add any number of constants in this class.

Did you know?
Each enum constant is an object.

Example –

enum class Color {
    GOLD, SILVER, WHITE, BLACK, RED
}

Here, we have defined an enum class Color with constants GOLD, SILVER, WHITE, BLACK and RED.

Kotlin Enum Initialization

Since each enum constant is an object of enum class, it can be initialized.

Let’s see an example to initialize enum

enum class Color(val value: Int) {
    GOLD(0xffd323),
    SILVER(0xeaeaea),
    WHITE(0xffffff),
    BLACK(0x000000),
    RED(0xFF0000)
}

Here, we have initialized GOLD with 0xffd323, SILVER with 0xeaeaea and so on.

Another example to initialize enum –

 enum class UserStatus(val value: Int) {
    PENDING (0),
    ACTIVE (200),
    INACTIVE (400),
    DELETED (500);
}

Here, we have created enum class UserStatus that contains four contants PENDING, ACTIVE, INACTIVE and DELETED. We have initialised PENDING with 0, ACTIVE with 200, INACTIVE with 400 and DELETED with 500.

How to use Enum in Kotlin

Now, Let’s see different examples that demonstrate how to use enum in kotlin

Example 1 –

enum class UserStatus(val value: Int) {
   PENDING (0),
   ACTIVE (200),
   INACTIVE (400),
   DELETED (500)
}

fun main(args: Array<String>) {
   println(UserStatus.PENDING)
}

When you run the above program, you will get output –
PENDING

Example 2 –

enum class UserStatus(val value: Int) {
   PENDING (0),
   ACTIVE (200),
   INACTIVE (400),
   DELETED (500)
}

fun main(args: Array<String>) {
   println(UserStatus.PENDING.value)
}

Output is –
0

UserStatus.PENDING.value will return the value of the enum object.

Exercises on Kotlin Enum Class

1. Guess the output of below program.

enum class UserStatus(val value: Int) {
   PENDING (0),
   ACTIVE (200),
   INACTIVE (400),
   DELETED (500)
}

fun main(args: Array<String>) {
   println(UserStatus.ACTIVE.value)
}

2. Guess the output of below program.

enum class UserStatus(val value: Int) {
   PENDING,
   ACTIVE,
   INACTIVE,
   DELETED
}

fun main(args: Array<String>) {
   println(UserStatus.ACTIVE.value)
}

3. Guess the output of below program.

enum class UserStatus {
   PENDING,
   ACTIVE,
   INACTIVE,
   DELETED
}

fun main(args: Array<String>) {
   println(UserStatus.ACTIVE.value)
}

4. Guess the output of below program.

enum class UserStatus {
   PENDING,
   ACTIVE,
   INACTIVE,
   DELETED
}

fun main(args: Array<String>) {
   println(UserStatus.ACTIVE)
}

That’s end of tutorial on Kotlin Enum.