129

Enum classes in Kotlin examples

 6 years ago
source link: http://developine.com/enum-classes-in-kotlin-example/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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) 
{
     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 :

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:

Category: KotlinTagged: enum iteration kotlinenums kotlin tutorialkotlin enum getvaluekotlin enum ordinalkotlin enum switchkotlin enum with when

edbf1a71af9f6b6d0fc1ed2155d02888?s=96&d=retro&r=g
Hammad Tariq

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK