Kotlin Enum Classes

Kotlin Enum – Enum is a special data type that allows a variable to hold a value only from a set of predefined constants.

In this tutorial, we shall learn about Kotlin Enum Class : Syntax, Initialization, how enum classes are different from regular classes. An example program is provided to demonstrate the usage of Kotlin Enum.


Following is the syntax to define an Enum class in Kotlin

enum class<enum_class_name> {

Any number of constants could be specified inside the class separated by comma. Each Enum constant is implicitly an Object of type <enum_class_name>.

Following is an example of defining a Kotlin Enum class “MobileColor” :


Kotlin Enum Initialization

As Kotlin Enum is an object of an Enum class, these could be initialized. We shall use the same example of MobileColor enum class to demonstrate the initialization of enums.

Kotlin Enum


Kotlin Enum Example – To demonstrate the usage of Enum Classes

Following example demonstrates the usage of enum class MobileColor. Data Class, Mobile, has a variable as Enum class, MobileColor Object.


How enum class is different from a regular class

You cannot create new instances of an Enum class outside of its definition. In other words outside enum definition, you cannot create an object of type Enum Class by passing values to its primary constructor. You may do so for regular classes.

Conclusion :

In this Kotlin TutorialKotlin Enum Classes, we have learnt the syntax and usage with help of an Example program.