Kotlin Enum
In this tutorial we will learn about Kotlin Enum. While developing an application, there may arise a situation where we want a variable to have a value out of a given set of allowed values only, for example, if we have a variable pizzaSize
, then it should have following values: small, medium and large.
In Kotlin, and in Java too, Enums help us achieve this.
In Kotlin we can create an enum class with the help of enum
keyword. Kotlin enums can have properties, functions, can implement interfaces, etc.
Kotlin Enum class
Let us create a simple enum class containing pizza sizes:
enum class pizza{
SMALL, MEDIUM, LARGE
}
Let us use this enum in main()
function:
fun main() {
val pizzaSize = pizza.LARGE
println("Size of pizza ordered is $pizzaSize")
}
Size of pizza ordered is LARGE
Each enum constant acts as a separate object. They are separated by commas. In above example, SMALL
, MEDIUM
and LARGE
are objects.
Kotlin Enum: Initializing Constants
In Kotlin enums can have primary constructors. As constants in enum are instances, they can be initialised by passing values to the constructor:
enum class pizza(val diameter: Int){
SMALL(10),
MEDIUM(12),
LARGE(14)
}
Let us use this enum in main()
function:
fun main() {
val pizzaSize = pizza.LARGE
println("Size of pizza ordered is $pizzaSize")
println("Diameter of LARGE pizza is ${pizzaSize.diameter}")
}
Size of pizza ordered is LARGE
Diameter of LARGE pizza is 14
As you can see in the example above, we have included more data in the Enum class and associated it with enum instances too. Similarly, we can add even more information if we want.
Kotlin Enum: Implementing Interface
In Kotlin, we can even implement an interface in an enum class. The constants seperately overrides the interface functions:
// interface for implementing
interface Price{
fun getPrice(): Int
}
// enum class
enum class pizza(val diameter: Int): Price{
// enum constant implementing interface function
SMALL(10){
override fun getPrice(): Int {
return 250
}
},
MEDIUM(12){
override fun getPrice(): Int {
return 450
}
},
LARGE(14){
override fun getPrice(): Int {
return 600
}
}
}
Now we can access these functions using the enum constants:
fun main() {
val pizzaSize = pizza.LARGE
println("Size of pizza ordered is $pizzaSize")
println("Diameter of LARGE pizza is ${pizzaSize.diameter}")
println("Price of LARGE pizza is ${pizzaSize.getPrice()}")
}
Size of pizza ordered is LARGE
Diameter of LARGE pizza is 14
Price of LARGE pizza is 600
So using this approach we can even provide function to our enum constants to provide any logic if required in the code.
By defining a Kotlin Interface first and then using it in our enum class, we can have more control over the enum constants we define and we can even include more information in them.
Kotlin Enums: values
and valueOf
functions
In Kotlin, we have two functions in each Enum by default. The values()
function returns an array containing all the constants of the enum class. Using the valueOf(name: String)
function, we can get the constant using the string value for the enum constant.
Let's use the values
and valueOf
function:
fun main() {
for(pizza in pizza.values()){
println(pizza)
}
val largePizza = pizza.valueOf("LARGE")
println("Diameter: ${largePizza.diameter}")
}
SMALL
MEDIUM
LARGE
Diameter: 14
In the code example above, we used the values()
function to get all the enum values, then used a for loop to iterate over the values and then finally we used the valueOf
function to get the enum constant using its string value.
Summary
In this tutorial we learned about Kotlin Enums and their usage. In the next tutorial we will discuss about Kotlin Sealed class.