An enum (enumerated) class in Kotlin is a specialized class used to model types that represent a finite, distinct set of constant values. Each constant defined within an enum class is an object instance of that enum type, making them singletons by nature. Because they are full-fledged classes, Kotlin enums can hold state, implement interfaces, and define custom behavior.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Instance Properties
The enum class itself implicitly inherits from thekotlin.Enum base class, and the constants are instances of that class. This base class provides standard properties for introspection on the individual instances:
name: AStringrepresenting the exact identifier of the enum constant as declared in the code.ordinal: AnIntrepresenting the zero-based position of the constant in the declaration order.
Synthetic Class Members
To facilitate constant retrieval, the Kotlin compiler generates synthetic members directly on the enum class itself. These act similarly to static members and are accessed via the class name, rather than through individual constant instances:entries: A pre-allocatedEnumEntries<E>list containing all constants in declaration order. Introduced in Kotlin 1.9, this is a memory-efficient replacement for the legacyvalues()array.valueOf(value: String): Returns the enum constant matching the specified string. Throws anIllegalArgumentExceptionif no match exists.values(): Returns anArray<E>of all enum constants (legacy approach, superseded byentries).
Generic Access
Kotlin provides standard library functions to access enum constants dynamically when the specific enum type is represented by a reified generic type parameterT. This is critical for generic programming where the exact enum class is not known at compile time:
enumEntries<T>(): Returns theEnumEntries<T>list for the given enum type. Note that this requires an explicit import fromkotlin.enums.enumValues<T>(): Returns anArray<T>of the enum constants.enumValueOf<T>(name: String): Returns the enum constant of typeTwith the specified name.
Constructors and Properties
Enum classes can declare a primary constructor to encapsulate state. Because each enum constant is an instance of the class, it must provide the required arguments to the primary constructor at the time of declaration.Custom Methods and Anonymous Classes
Enum classes can declare member functions. Furthermore, an enum class can declareabstract functions, forcing each constant to provide its own implementation via an anonymous class.
When defining member functions inside an enum class, the list of enum constants must be terminated with a semicolon (;).
Interface Implementation
Enum classes cannot inherit from other classes (as they already implicitly inherit fromkotlin.Enum), but they can implement one or more interfaces. The interface methods can be implemented globally by the enum class itself, or overridden individually by each constant’s anonymous class.
Master Kotlin with Deep Grasping Methodology!Learn More





