Enum classes in Kotlin Tutorial

In this tutorial, we will learn about Enum classes in Kotlin language.

we will learn how to declare Enum class and how to initialize Enum classes in Kotlin.

What is Enum Class in Kotlin?

Enum in Kotlin is a custom data type that can contain a set of constants.

In the Kotlin programming language, we define an Enum using keyword “enum“.

The syntax of Enum Class in Kotlin:

enum class ClassName {
   ConstantA, ConstantB, ConstantC
}

Common examples of Enum include days of the week, compass directions (North, East, West, South).

Anytime you need fixed set of constants you can use Enums in Kotlin.

Each enum constant/object

This is how we define Enum class in Kotlin

enum class ColorEnum {
     Red, Green, Blue, Orange 
 }

we can define any number of constants in an Enum class in Kotlin.

Constants declared inside Enum class are the object of Colors class.

How to initialize Enum class in Kotlin

As each constant inside class is an object of Enum class.

We can initialize constants in enum class in Kotlin like below.

// (val colorCode:Int) is primary constructor
//  of our enum class.
enum class ColorEnum(val colorCode: Int) 
{
     Red(2),
     Blue(11212),
     Green(21212),
     Orange(212121)
 } 

In above code snippet, we have assigned a value to each instance of enum class. 

We also added the primary constructor in our enum class to initialize objects.

As each object/constant in our enum class needs one argument/parameter for initialization, so we need to provide a primary constructor.

How to iterate Enum class in Kotlin using for loop example

Above Enum class can be traversed using this code snippet

for (enum in ColorEnum.values()) {
     println(enum)
 }

The output of above code will be :

RED

BLUE

GREEN

ORANGE

values() method in Enum class in Kotlin returns an array of all the enum values, the compiler adds values method internally.

Each Enum Object has its own properties.

println(ColorEnum.valueOf("Orange"))

The output of above program will be:

Orange

Properties of Enum class

Each enum class has two properties:

  1. ordinal   // returns index of an enum object.
  2. name    // returns an object name.

Getting the index of Enum Object using ordinal property in Enum class in Kotlin

println(ColorEnum.valueOf("Orange").ordinal)

Above code snippet will print 3 as the index of Orange. Note: index always starts from zero.

Using “when” to deal with Enum classes

we can apply “when” on Enum classes, “when” in Kotlin is used as an alternate of “switch” statement in Java.

var color = ColorEnum.Green
 
 when (color) {
     
     ColorEnum.Green -> {
         println("I am Green Color")
     }
     ColorEnum.Blue -> {
         println("I am Blue Color")
     }
     ColorEnum.Orange -> {
         println("I am Orange Color")
     }
     ColorEnum.Red -> {
         println("I am Red Color")
     }
     
 }

This is how “when” statement in Kotlin can be used with enums.

Conclusion:

we have learned How to declare enum classes in Kotlin, How to initialize enum classes, How to iterate through enum class, How to use when statement with enum class in Kotlin.

Recommended Reading:

Contact Us