![]() |
VOOZH | about |
In programming, sometimes we want a variable to have only a few specific values. For example, days of the week or card suits (like Heart, Spade, etc.). To make this possible, most programming languages support something called enumeration or enum.
Enums are a list of named constants. Kotlin supports enums in a very powerful way, and unlike some other languages like java, Kotlin enums are actually classes. This means they can have properties, methods, and even constructors.
An enum (short for enumeration) is a special type in Kotlin that holds a fixed set of constant values. Each value is like an object, and you cannot create new instances of them.
For example:
In this example, DAYS is an enum class with 7 values, one for each day of the week.
In Kotlin also enums can have a constructor like Java enums. Since enum constants are instances of an Enum class, the constants can be initialized by passing specific values to the primary constructor.
Here is an example of specifying colors for cards -
Here, each card has a color. You can access the color like this:
val color = Cards.Diamond.color
println(color)
Output:
blackSimilar to Java and in other programming languages, Kotlin enum classes has some inbuilt properties and functions which can be used by the programmer. Here's a look at the major properties and methods.
Properties -
Methods -
Example to demonstrate enum class in Kotlin
Output:
0 = SUNDAY
1 = MONDAY
2 = TUESDAY
3 = WEDNESDAY
4 = THURSDAY
5 = FRIDAY
6 = SATURDAY
WEDNESDAY
Since enum class in Kotlin, defines a new type. This class type can have its own properties and functions. The properties can be given a default value, however, if not provided, then each constant should define its own value for the property. In the case of functions, they are usually defined within the companion objects so that they do not depend on specific instances of the class. However, they can be defined without companion objects also.
Example to demonstrate properties and functions in Kotlin
Output:
0 = SUNDAY and is weekend true
1 = MONDAY and is weekend false
2 = TUESDAY and is weekend false
3 = WEDNESDAY and is weekend false
4 = THURSDAY and is weekend false
5 = FRIDAY and is weekend false
6 = SATURDAY and is weekend true
Is today a weekend false
Enum constants also behaves as anonymous classes by implementing their own functions along with overriding the abstract functions of the class. The most important thing is that each enum constant must be override.
This can called as
println(Weather.SUMMER.description())Output:
Hot days of a yearA great advantage of enum classes in Kotlin comes into play when they are combined with the when expression. The advantage is since enum classes restrict the value a type can take, so when used with the when expression and the definition for all the constants are provided, then the need of the else clause is completely eliminated. In fact, doing so will generate a warning from the compiler.
Example:
If you forget a day, the compiler will warn you.