October 2018
Intermediate to advanced
370 pages
9h 15m
English
In Kotlin, enum classes are similar to sealed classes, except that all the values of the enum class are the same type. The enum class is useful when the expected outcome is within a small set, such as a small range of colours or the days of the week. Let's create a few examples of enum classes and see how they work:
enum class Color { RED, GREEN, BROWN, YELLOW }
The enum keyword is used to declare the enum class. Each value can be declared with a comma-separated list.
Each value of the enum class can be accessed by using the name of the enum class. As we can see in the preceding example, enum Color contains four colors. We can access the name and value of each color using the member name and the ordinal property:
fun main(args: ...
Read now
Unlock full access