0Pricing
Kotlin Academy · Lesson

Inner Classes

Access outer scope.

Inner Classes 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.

The inner Keyword

Marking a nested class inner gives it a reference to the outer instance, so it can access the outer class(rsquo)s members.

class Outer {
    val msg = "Hello"
    inner class Inner {
        fun show() = msg
    }
}

fun main() {
    println(Outer().Inner().show())
}

Creating an Inner Instance

Because an inner class needs an outer instance, you create it from one: Outer().Inner().

class Engine {
    val power = 100
    inner class Turbo {
        fun boosted() = power * 2
    }
}

fun main() {
    println(Engine().Turbo().boosted())
}

Accessing Outer Properties

An inner class reads and uses outer properties directly, as if they were its own.

class Counter {
    var count = 0
    inner class Stepper {
        fun step() { count++ }
    }
}

fun main() {
    val c = Counter()
    c.Stepper().step()
    println(c.count)
}

Calling Outer Methods

Inner classes can call methods on the enclosing instance too.

class Printer {
    fun line(s: String) = println(s)
    inner class Job {
        fun run() = line("printing")
    }
}

fun main() {
    Printer().Job().run()
}

The this@Outer Syntax

To refer to the outer instance explicitly (for example when names clash), use this@Outer.

class Outer {
    val value = 10
    inner class Inner {
        val value = 20
        fun outerValue() = this@Outer.value
    }
}

fun main() {
    println(Outer().Inner().outerValue())
}

Why Use inner?

Use inner when the nested type genuinely needs to collaborate with a specific outer instance, sharing its state.

class Wallet {
    var balance = 50
    inner class Transaction(val amount: Int) {
        fun apply() { balance += amount }
    }
}

fun main() {
    val w = Wallet()
    w.Transaction(25).apply()
    println(w.balance)
}

Each Inner Bound to One Outer

An inner instance is tied to the exact outer instance that created it; changes flow to that object.

class Box(var size: Int) {
    inner class Resizer {
        fun grow() { size += 1 }
    }
}

fun main() {
    val b = Box(3)
    b.Resizer().grow()
    println(b.size)
}

Inner vs Nested

Without inner, a nested class is isolated. With inner, it captures the outer instance. The keyword is the whole difference.

class Outer {
    val tag = "X"
    inner class A { fun get() = tag }
}

fun main() {
    println(Outer().A().get())
}

Memory Note

Because an inner instance holds the outer instance alive, be mindful with long-lived inner objects to avoid unintentionally retaining the outer object.

class Service {
    val id = 1
    inner class Worker { fun who() = id }
}

fun main() {
    println(Service().Worker().who())
}

A Realistic Iterator

An inner class is ideal for an iterator that walks its enclosing collection(rsquo)s state.

class Stack {
    private val items = mutableListOf(1, 2, 3)
    inner class Reader {
        fun all() = items.joinToString()
    }
}

fun main() {
    println(Stack().Reader().all())
}

Combining the Concepts

Inner classes can use outer state and disambiguate with this@Outer.

class Outer {
    val n = 5
    inner class Inner {
        fun doubled() = this@Outer.n * 2
    }
}

fun main() {
    println(Outer().Inner().doubled())
}

Quick Check

Test your understanding of inner classes.

Recap

You learned about inner classes:

  • inner grants access to the outer instance and its members.
  • Create via an outer instance: Outer().Inner().
  • Use this@Outer to reference the enclosing instance.
  • Inner instances keep their outer instance alive.
class Outer {
    val v = 1
    inner class Inner { fun get() = v }
}

fun main() {
    println(Outer().Inner().get())
}

Frequently asked questions

Is the “Inner Classes” lesson free?

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

Access outer scope. 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 “Inner 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