Introduction to Enum Classes
Learn how to define and use enum classes and access enum properties and functions.
In this chapter, we’re going to introduce the concept of enum
classes. Let’s start with an example. Suppose that we’re implementing a payment method that has to support three possible options:
Cash payment
Card payment
Bank transfer
Using enum
values
The most basic way to represent a fixed set of values in Kotlin is an enum
class. Inside its body, we list all the values, divided by a comma.
We name values using UPPER_SNAKE_CASE notation (e.g.,
BANK_TRANSFER
).Enum class elements can be referenced by the
enum
name, followed by a dot, and then the value name (e.g.,PaymentOption.CASH
).All values are typed as the
enum
class type.
Get hands-on with 1400+ tech skills courses.