0Pricing
Kotlin Academy · Lesson

Nested Classes

Classes inside classes.

Nested Classes is a free Kotlin Academy lesson on CoddyKit — lesson 1 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 Inside Classes

Kotlin lets you declare a class inside another class. By default such a class is nested and does not hold a reference to the outer instance.

class Outer {
    class Nested {
        fun hello() = "Hi from Nested"
    }
}

fun main() {
    println(Outer.Nested().hello())
}

Creating a Nested Instance

Because a nested class is independent, you create it via the outer class name without an outer instance: Outer.Nested().

class Computer {
    class Cpu {
        val cores = 8
    }
}

fun main() {
    val cpu = Computer.Cpu()
    println(cpu.cores)
}

No Access to Outer Members

A nested class cannot see the outer class(rsquo)s instance members because it has no outer reference. It behaves like a top-level class scoped under a name.

class Bank {
    val name = "ACME"
    class Branch {
        fun describe() = "A branch"
    }
}

fun main() {
    println(Bank.Branch().describe())
}

Why Nest at All?

Nesting expresses a logical grouping: the inner type belongs conceptually to the outer one, improving organization and namespacing.

class Http {
    class Request(val url: String)
}

fun main() {
    val r = Http.Request("/home")
    println(r.url)
}

Nested Class with Properties

Nested classes are ordinary classes: they can have constructors, properties, and methods.

class Garage {
    class Car(val model: String) {
        fun honk() = "$model: beep"
    }
}

fun main() {
    println(Garage.Car("Tesla").honk())
}

Accessing Companion or Static-like Members

Nested classes can be referenced from anywhere using the outer name, similar to a static nested class in Java.

class Config {
    class Defaults {
        val timeout = 30
    }
}

fun main() {
    println(Config.Defaults().timeout)
}

Multiple Nested Classes

An outer class can contain several nested classes to group related types together.

class Shapes {
    class Circle(val r: Int)
    class Square(val side: Int)
}

fun main() {
    println(Shapes.Circle(5).r)
    println(Shapes.Square(4).side)
}

Nested Inside Nested

You can nest more than one level deep, though deep nesting can hurt readability.

class A {
    class B {
        class C {
            val value = 42
        }
    }
}

fun main() {
    println(A.B.C().value)
}

Nested vs Top-Level

Functionally a nested class is like a top-level class; the only difference is its qualified name. Choose nesting when the relationship aids clarity.

class Order {
    class Item(val price: Int)
}

fun main() {
    val total = Order.Item(10).price + Order.Item(5).price
    println(total)
}

A Practical Grouping

Grouping a builder result type under its owner reads naturally.

class Parser {
    class Result(val ok: Boolean, val message: String)

    fun parse(text: String) = Result(text.isNotEmpty(), "parsed")
}

fun main() {
    val res = Parser().parse("data")
    println("${res.ok} ${res.message}")
}

Key Rule to Remember

The keyword that makes a class hold an outer reference is inner. Without it, a nested class is static-like and isolated.

class Box {
    class Lid
}

fun main() {
    println(Box.Lid() != null)
}

Quick Check

Test your understanding of nested classes.

Recap

You learned about nested classes:

  • Declared inside another class, default is class (nested).
  • No reference to the outer instance; cannot access outer members.
  • Created via the outer name: Outer.Nested().
  • Great for logical grouping and namespacing.
class Outer {
    class Nested { val x = 1 }
}

fun main() {
    println(Outer.Nested().x)
}

Frequently asked questions

Is the “Nested Classes” lesson free?

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

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

How long does the “Nested 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