0
0
Kotlinprogramming~10 mins

Inner classes and nested classes in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Inner classes and nested classes
Define Outer Class
Define Nested Class (static)
Define Inner Class (non-static)
Create Outer Instance
Create Inner Instance (needs Outer)
Create Nested Instance (no Outer needed)
Access members
End
Shows how Kotlin defines outer, nested (static), and inner (non-static) classes and how instances are created and accessed.
Execution Sample
Kotlin
class Outer {
    val outerVal = "Outer"
    class Nested {
        fun nestedFun() = "Nested"
    }
    inner class Inner {
        fun innerFun() = "Inner and $outerVal"
    }
}

fun main() {
    val nested = Outer.Nested()
    val inner = Outer().Inner()
    println(nested.nestedFun())
    println(inner.innerFun())
}
Creates an Outer class with nested and inner classes, then calls their functions showing access differences.
Execution Table
StepActionEvaluationResult
1Define class OuterOuter class createdOuter class ready
2Define Nested class inside OuterNested class created (static)Nested class ready
3Define Inner class inside OuterInner class created (non-static)Inner class ready
4Create instance nested = Outer.Nested()Nested instance creatednested is Outer.Nested instance
5Create instance inner = Outer().Inner()Outer instance created, then Inner instance linkedinner is Outer.Inner instance linked to Outer instance
6Call nested.nestedFun()Returns "Nested"Output: Nested
7Call inner.innerFun()Returns "Inner and Outer"Output: Inner and Outer
8Program endsNo more actionsExecution stops
💡 Program ends after printing outputs from nested and inner class functions.
Variable Tracker
VariableStartAfter Step 4After Step 5Final
nestedundefinedOuter.Nested instanceOuter.Nested instanceOuter.Nested instance
innerundefinedundefinedOuter.Inner instance linked to Outer instanceOuter.Inner instance linked to Outer instance
Key Moments - 2 Insights
Why does Inner class need an Outer instance but Nested class does not?
Inner class is non-static and holds a reference to its Outer instance, so it can access Outer members (see Step 5 and 7). Nested class is static and independent, created without Outer instance (see Step 4).
How does inner.innerFun() access outerVal from Outer?
Because Inner class is linked to an Outer instance, it can access Outer properties directly (Step 7 shows innerFun returns "Inner and Outer"). Nested class cannot do this.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of nested.nestedFun() at Step 6?
A"Inner and Outer"
B"Nested"
C"Outer"
DError
💡 Hint
Check Step 6 in the execution_table where nested.nestedFun() returns "Nested".
At which step is the Inner instance created and linked to an Outer instance?
AStep 5
BStep 4
CStep 6
DStep 7
💡 Hint
Look at Step 5 in execution_table where inner = Outer().Inner() is created.
If we remove 'inner' keyword from Inner class, what changes in the execution?
AOuter instance is not needed for Inner creation
Bnested.nestedFun() will fail
Cinner.innerFun() can no longer access outerVal
DNo change at all
💡 Hint
Refer to key_moments about Inner class needing Outer instance to access outerVal.
Concept Snapshot
Kotlin classes can have nested and inner classes.
Nested classes are static and don't access Outer members.
Inner classes are non-static and can access Outer members.
Create Nested with Outer.Nested(), Inner with Outer().Inner().
Inner holds reference to Outer instance; Nested does not.
Full Transcript
This example shows Kotlin's Outer class with two inner classes: Nested and Inner. Nested is static, so it can be created without an Outer instance and cannot access Outer properties. Inner is non-static and requires an Outer instance to be created; it can access Outer properties. The execution table traces defining classes, creating instances, and calling their functions. Nested.nestedFun() returns "Nested" while inner.innerFun() returns "Inner and Outer" showing access to Outer. Key moments clarify why Inner needs Outer instance and how access works. The quiz tests understanding of outputs, creation steps, and effects of removing 'inner' keyword.