0
0
KotlinComparisonBeginner · 4 min read

Inner vs Nested Class in Kotlin: Key Differences and Usage

In Kotlin, a nested class is a static class that cannot access members of its outer class, while an inner class is non-static and can access the outer class's members. You declare an inner class with the inner keyword, enabling it to hold a reference to the outer class instance.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of inner and nested classes in Kotlin.

AspectNested ClassInner Class
DefinitionStatic class inside another classNon-static class inside another class
Access to outer class membersNo accessCan access outer class members
Keyword usedNo keyword neededinner keyword required
Reference to outer classNo referenceHolds reference to outer class instance
Instance creationCan be created without outer class instanceRequires outer class instance to create
Use caseGrouping without outer contextAccessing outer class data
⚖️

Key Differences

A nested class in Kotlin is like a static class in Java. It does not hold a reference to the outer class, so it cannot access the outer class's properties or functions directly. This makes it lightweight and useful when you want to group classes logically without needing the outer class context.

On the other hand, an inner class is marked with the inner keyword. It keeps a reference to an instance of the outer class, allowing it to access the outer class's members directly. This is helpful when the inner class needs to work closely with the outer class's data or behavior.

Because an inner class holds a reference to the outer class, you must create an instance of the outer class first before creating the inner class instance. Nested classes do not have this requirement and can be instantiated independently.

⚖️

Code Comparison

kotlin
class Outer {
    private val outerValue = "Outer"

    class Nested {
        fun nestedFunction() = "Nested class cannot access outerValue"
    }

    inner class Inner {
        fun innerFunction() = "Inner class can access: $outerValue"
    }
}

fun main() {
    val nested = Outer.Nested()
    println(nested.nestedFunction())

    val outer = Outer()
    val inner = outer.Inner()
    println(inner.innerFunction())
}
Output
Nested class cannot access outerValue Inner class can access: Outer
↔️

Inner Class Equivalent

kotlin
class Outer {
    private val outerValue = "Outer"

    inner class Inner {
        fun innerFunction() = "Inner class can access: $outerValue"
    }
}

fun main() {
    val outer = Outer()
    val inner = outer.Inner()
    println(inner.innerFunction())
}
Output
Inner class can access: Outer
🎯

When to Use Which

Choose a nested class when you want to group a class inside another without needing access to the outer class's properties or functions. It is simpler and does not hold a reference to the outer class, saving memory.

Choose an inner class when the inner class needs to access or modify the outer class's members. This is useful for tightly coupled logic where the inner class depends on the outer class instance.

Key Takeaways

Nested classes are static and cannot access outer class members.
Inner classes hold a reference to the outer class and can access its members.
Use nested classes for logical grouping without outer context dependency.
Use inner classes when inner needs to interact with outer class data.
Inner classes require an outer class instance to be created.