0Pricing
Kotlin Academy · Lesson

Local Classes

Classes in functions.

Local Classes is a free Kotlin Academy lesson on CoddyKit — lesson 4 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.

Classes in Functions

Kotlin lets you declare a local class inside a function body. It is visible only within that function.

fun main() {
    class Greeting(val text: String) {
        fun show() = println(text)
    }
    Greeting("Hello local").show()
}

Scoped to the Function

A local class cannot be used outside the function where it is declared, keeping helper types tightly contained.

fun build(): String {
    class Builder {
        fun make() = "built"
    }
    return Builder().make()
}

fun main() {
    println(build())
}

Capturing Local Variables

Like closures, local classes can capture variables from the enclosing function.

fun main() {
    val prefix = ">> "
    class Logger {
        fun log(msg: String) = println(prefix + msg)
    }
    Logger().log("started")
}

Multiple Instances

You can create as many instances of a local class as you like within the function.

fun main() {
    class Point(val x: Int)
    val a = Point(1)
    val b = Point(2)
    println(a.x + b.x)
}

Local Class with Methods

Local classes are full classes: constructors, properties, and methods all work.

fun main() {
    class Account(var balance: Int) {
        fun deposit(n: Int) { balance += n }
    }
    val acc = Account(100)
    acc.deposit(50)
    println(acc.balance)
}

Why Use a Local Class?

Use one when a helper type is only meaningful inside a single function, avoiding cluttering the file with top-level types.

fun parseLine(line: String): Int {
    class Token(val value: String)
    val tokens = line.split(",").map { Token(it) }
    return tokens.size
}

fun main() {
    println(parseLine("a,b,c"))
}

Capturing Mutable State

A local class may modify captured mutable variables from its enclosing scope.

fun main() {
    var total = 0
    class Adder {
        fun add(n: Int) { total += n }
    }
    val a = Adder()
    a.add(5)
    a.add(7)
    println(total)
}

Implementing Interfaces Locally

Local classes can implement interfaces, useful when you need a named, reusable implementation inside one function.

interface Step { fun run(): String }

fun main() {
    class First : Step {
        override fun run() = "first"
    }
    println(First().run())
}

Local vs Anonymous

Choose a local class when you need a name (to create multiple instances or reference the type). Choose an anonymous object for a single inline instance.

fun main() {
    class Repeater(val times: Int) {
        fun run() = "x".repeat(times)
    }
    println(Repeater(3).run())
}

Nesting Inside Loops

A local class can even be declared inside blocks like loops, scoped to that block.

fun main() {
    for (i in 1..2) {
        class Tag(val id: Int)
        println(Tag(i).id)
    }
}

A Practical Helper

Local classes shine for short-lived domain helpers used by exactly one function.

fun report(values: List<Int>): String {
    class Stats(val sum: Int, val max: Int)
    val s = Stats(values.sum(), values.max())
    return "sum=${s.sum} max=${s.max}"
}

fun main() {
    println(report(listOf(3, 7, 2)))
}

Quick Check

Test your understanding of local classes.

Recap

You learned about local classes:

  • Declared inside a function and scoped to it.
  • Can capture (and modify) enclosing local variables.
  • Full classes: constructors, properties, methods, interfaces.
  • Use a local class when you need a name; an anonymous object for a single inline instance.
fun main() {
    val tag = "L"
    class C { fun show() = tag }
    println(C().show())
}

Frequently asked questions

Is the “Local Classes” lesson free?

Yes — the full text of “Local Classes” 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 “Local Classes”?

Classes in functions. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Local Classes” 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. Nested Classes
  2. Inner Classes
  3. Anonymous Objects
  4. Local Classes
← Back to Kotlin Academy