Ad space (banner)
๐ŸŽฏKotlin Lessons
Lesson 41 / 68

Enum (Enumerated Types) Basics

In this lesson you will learn the basics of enums in Kotlin, so a fixed set of options can be referred to by readable names. This is for anyone searching for "Kotlin enum class how to use".

enum class creates an enumerated type representing a fixed set of options. Use it when you want to limit the possible values in advance, the way a traffic light only has red, green, and amber.

The sample code defines three options with enum class Color { RED, GREEN, BLUE } and assigns one of them to a variable as Color.RED. Notice how Color.values() lists every value in the enum.

A common stumbling block for beginners is not knowing that an enum class can carry properties and methods. A Kotlin enum is not just a list of constants - it can behave like an ordinary class, which makes it a powerful feature.

In real-world development enum classes are common wherever you handle a fixed set of options, such as order statuses or days of the week, and they make the code far more readable.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Kotlin directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)