Introduction
An enum class in Kotlin represents a fixed collection of constants, where each constant is a singleton instance of the enum type. Enums are useful whenever a value can only take one of a limited number of known options, such as directions, days of the week, or order statuses. Unlike plain constants, enum constants can carry properties and methods, making them more powerful than in many other languages. Each constant listed in an enum class is a singleton object of that enum type. Enum classes can define a constructor and properties, letting each constant carry its own data. They can also declare abstract methods and provide a different implementation for each constant using a body block. Kotlin provides built-in support via values() (or entries in newer versions) to list all constants, valueOf(name) to look one up by name, plus .ordinal for its position and .name for its identifier.
Cricket analogy: Just as a match result can only be WIN, LOSS, or TIE (a fixed enum of outcomes), each match-day squad slot like WICKETKEEPER is a singleton role carrying its own stats such as MS Dhoni's keeping average, not just a bare label.
Syntax
enum class Direction {
NORTH, SOUTH, EAST, WEST
}Example
enum class Op {
PLUS {
override fun apply(a: Int, b: Int) = a + b
},
MINUS {
override fun apply(a: Int, b: Int) = a - b
};
abstract fun apply(a: Int, b: Int): Int
}
fun main() {
println(Op.PLUS.apply(3, 4))
println(Op.MINUS.apply(10, 6))
println(Op.PLUS.name)
println(Op.PLUS.ordinal)
}Output
7
4
PLUS
0Key Takeaways
- Each enum constant is a singleton instance of the enum class.
- Enum classes can have constructors, properties, and methods.
- Individual constants can override abstract methods with their own implementation.
- values()/entries and valueOf() let you enumerate and look up constants.
- .ordinal gives position and .name gives the constant's identifier.
Practice what you learned
1. What is each constant of an enum class in Kotlin?
2. Which built-in property returns the position of a constant in its declaration order?
3. Can enum constants have different implementations of the same abstract method?
4. Which function is used to look up an enum constant by its string name?
5. What can an enum class have besides plain constants?
Was this page helpful?
You May Also Like
Sealed Classes in Kotlin
Sealed classes restrict a class hierarchy to a known, closed set of subclasses, enabling exhaustive when checks.
when Expression in Kotlin
Explore Kotlin's when expression, a powerful and flexible replacement for Java's switch statement.
Classes in Kotlin
Learn how Kotlin classes bundle state and behavior with concise constructor syntax and final-by-default semantics.
Constructors in Kotlin (Primary and Secondary)
Understand how primary and secondary constructors initialize Kotlin objects, including init blocks and constructor delegation.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics