The most basic use case for enum classes is the implementation of type-safe enums:
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
Each enum constant is an object. Enum constants are separated by commas.
Since each enum is an instance of the enum class, it can be initialized as:
enum class Color(val rgb: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
Enum constants can declare their own anonymous classes with their corresponding methods, as well as with overriding base methods.
enum class ProtocolState {
WAITING {
override fun signal() = TALKING
},
TALKING {
override fun signal() = WAITING
};
abstract fun signal(): ProtocolState
}
If the enum class defines any members, separate the constant definitions from the member definitions with a semicolon.
An enum class can implement an interface (but it cannot derive from a class), providing either a common implementation of interface members for all the entries, or separate implementations for each entry within its anonymous class. This is done by adding the interfaces you want to implement to the enum class declaration as follows:
import java.util.function.BinaryOperator
import java.util.function.IntBinaryOperator
//sampleStart
enum class IntArithmetics : BinaryOperator<Int>, IntBinaryOperator {
PLUS {
override fun apply(t: Int, u: Int): Int = t + u
},
TIMES {
override fun apply(t: Int, u: Int): Int = t * u
};
override fun applyAsInt(t: Int, u: Int) = apply(t, u)
}
//sampleEnd
fun main() {
val a = 13
val b = 31
for (f in IntArithmetics.values()) {
println("$f($a, $b) = ${f.apply(a, b)}")
}
}
{kotlin-runnable="true"}
All enum classes implement the Comparable interface by default. Constants in the enum class are defined in the natural order. For more information, see Ordering.
Enum classes in Kotlin have synthetic methods for listing the defined enum constants and getting an enum constant by
its name. The signatures of these methods are as follows (assuming the name of the enum class is EnumClass
):
EnumClass.valueOf(value: String): EnumClass
EnumClass.values(): Array<EnumClass>
Below is an example of these methods in action:
enum class RGB { RED, GREEN, BLUE }
fun main() {
for (color in RGB.values()) println(color.toString()) // prints RED, GREEN, BLUE
println("The first color is: ${RGB.valueOf("RED")}") // prints "The first color is: RED"
}
{kotlin-runnable="true" kotlin-min-compiler-version="1.3" id="rgb-enums-kotlin"}
The valueOf()
method throws an IllegalArgumentException
if the specified name does
not match any of the enum constants defined in the class.
In Kotlin 1.9.0, the entries
property is introduced as a replacement for the values()
function. The
entries
property returns a pre-allocated immutable list of your enum constants. This is particularly useful when you
are working with collections and can help you avoid performance issues.
For example:
enum class RGB { RED, GREEN, BLUE }
fun main() {
for (color in RGB.entries) println(color.toString())
// prints RED, GREEN, BLUE
}
Every enum constant also has properties: name
and ordinal
, for obtaining its name and
position (starting from 0) in the enum class declaration:
enum class RGB { RED, GREEN, BLUE }
fun main() {
//sampleStart
println(RGB.RED.name) // prints RED
println(RGB.RED.ordinal) // prints 0
//sampleEnd
}
{kotlin-runnable="true" kotlin-min-compiler-version="1.3" id="rgb-enums-properties-kotlin"}
You can access the constants in an enum class in a generic way using
the enumValues<T>()
and enumValueOf<T>()
functions:
enum class RGB { RED, GREEN, BLUE }
inline fun <reified T : Enum<T>> printAllValues() {
println(enumValues<T>().joinToString { it.name })
}
printAllValues<RGB>() // prints RED, GREEN, BLUE
For more information about inline functions and reified type parameters, see Inline functions.
{type="tip"}
In Kotlin 1.9.20, the enumEntries<T>()
function is introduced as a future replacement for the enumValues<T>()
function.
The enumValues<T>()
function is still supported, but we recommend that you use the enumEntries<T>()
function instead
because it has less performance impact. Every time you call enumValues<T>()
a new array is created, whereas whenever
you call enumEntries<T>()
the same list is returned each time, which is far more efficient.
For example:
enum class RGB { RED, GREEN, BLUE }
@OptIn(ExperimentalStdlibApi::class)
inline fun <reified T : Enum<T>> printAllValues() {
println(enumEntries<T>().joinToString { it.name })
}
printAllValues<RGB>()
// RED, GREEN, BLUE
The
enumEntries<T>()
function is Experimental. To use it, opt in with@OptIn(ExperimentalStdlibApi)
, and set the language version to at least 1.9.
{type="warning"}