Kotlin Enum Classes tutorial

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) 

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()) {

The output of above code will be :





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.


The output of above program will be:


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


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.


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:

Hello! I am Hammad Tariq. Jack of All trades and Yet master of Many! Senior Android Application Developer | Cross-platform Development | Flutter | Backend Development (Spring Boot + Kotlin + MySQL) | Blogger | Founder I am an all-around Mobile Developer who is passionate about learning the latest technologies and trends. I graduated from Air University Islamabad, Pakistan in 2015 but started working on Android Development, Universal Windows Apps development long before that. Currently, I am working as a team lead and Senior Android Developer at EVAMP SAANGA, Pakistan.
Posts created 43

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top

Contact Us