0Pricing
Kotlin Academy · Lesson

Enum Properties and Methods

Add behavior.

Enum Properties and Methods is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Enums Can Hold Data

Enum constants can carry properties. Declare a constructor on the enum class and pass values when listing each constant.

enum class Coin(val cents: Int) {
    PENNY(1), NICKEL(5), DIME(10)
}

fun main() {
    println(Coin.DIME.cents)
}

Constructor Parameters

The values in parentheses after each constant are arguments to the enum constructor. They become read-only properties on that constant.

enum class Planet(val gravity: Double) {
    EARTH(9.8), MOON(1.6)
}

fun main() {
    println(Planet.MOON.gravity)
}

Multiple Properties

An enum can declare several properties at once.

enum class Fruit(val color: String, val calories: Int) {
    APPLE("red", 95),
    BANANA("yellow", 105)
}

fun main() {
    val f = Fruit.BANANA
    println("${f.color} ${f.calories}")
}

Adding Methods

You can declare regular functions in the enum body. Every constant shares them.

enum class Temp(val celsius: Int) {
    COLD(5), WARM(25);

    fun toFahrenheit(): Int = celsius * 9 / 5 + 32
}

fun main() {
    println(Temp.WARM.toFahrenheit())
}

The Semicolon Rule

When an enum has members beyond the constants (methods or properties), you must place a semicolon after the last constant.

enum class Op {
    ADD, SUB;

    fun describe() = "Operation: $name"
}

fun main() {
    println(Op.ADD.describe())
}

Abstract Methods Per Constant

Enums can declare abstract methods that each constant overrides, giving every constant its own behavior.

enum class Math {
    PLUS { override fun apply(a: Int, b: Int) = a + b },
    TIMES { override fun apply(a: Int, b: Int) = a * b };

    abstract fun apply(a: Int, b: Int): Int
}

fun main() {
    println(Math.TIMES.apply(3, 4))
}

Computed Properties

Property getters can compute values based on the constant(rsquo)s data.

enum class Account(val balance: Int) {
    SAVINGS(1000), CHECKING(50);

    val isRich: Boolean get() = balance > 500
}

fun main() {
    println(Account.SAVINGS.isRich)
}

Combining Data and Behavior

Properties plus methods let an enum model a rich, closed domain.

enum class Shape(val sides: Int) {
    TRIANGLE(3), SQUARE(4);

    fun describe() = "$name has $sides sides"
}

fun main() {
    println(Shape.SQUARE.describe())
}

Using Properties in Logic

Constant properties are handy in calculations and conditions.

enum class Tier(val discount: Int) {
    BRONZE(5), GOLD(20)
}

fun price(base: Int, tier: Tier) = base - base * tier.discount / 100

fun main() {
    println(price(100, Tier.GOLD))
}

Default Property Values

Enum constructors support default parameter values like any constructor.

enum class Role(val canEdit: Boolean = false) {
    VIEWER, EDITOR(true)
}

fun main() {
    println(Role.VIEWER.canEdit)
    println(Role.EDITOR.canEdit)
}

A Complete Model

Here an enum models HTTP statuses with both data and behavior.

enum class Http(val code: Int) {
    OK(200), NOT_FOUND(404);

    fun isError() = code >= 400
}

fun main() {
    println(Http.NOT_FOUND.isError())
}

Quick Check

Test your understanding of enum properties and methods.

Recap

You learned that enums can carry behavior:

  • Add a constructor to give constants properties.
  • Declare shared methods (use a semicolon after constants).
  • Abstract methods let each constant define its own behavior.
  • Computed and default-valued properties work as usual.
enum class Coin(val cents: Int) {
    PENNY(1), DIME(10);
    fun double() = cents * 2
}

fun main() {
    println(Coin.DIME.double())
}

Frequently asked questions

Is the “Enum Properties and Methods” lesson free?

Yes — the full text of “Enum Properties and Methods” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Enum Properties and Methods”?

Add behavior. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Enum Properties and Methods” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining Enums
  2. Enum Properties and Methods
  3. Enums with when
  4. Enum Values and Ordinals
← Back to Kotlin Academy