0
0
Kotlinprogramming~10 mins

Inner class access to outer members in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Inner class access to outer members
Create Outer Class Instance
Create Inner Class Instance
Inner accesses Outer member
Print or use Outer member value
End
The inner class is created inside the outer class and can access the outer class's members directly.
Execution Sample
Kotlin
class Outer {
    val outerVal = "Hello from Outer"
    inner class Inner {
        fun printOuter() = println(outerVal)
    }
}
fun main() {
    val inner = Outer().Inner()
    inner.printOuter()
}
This code creates an outer class with an inner class that accesses and prints a member of the outer class.
Execution Table
StepActionEvaluationResult
1Create Outer instanceOuter instance createdouterVal = "Hello from Outer"
2Create Inner instance from OuterInner instance createdCan access outerVal
3Call Inner.printOuter()Access outerVal from OuterPrints: Hello from Outer
4End of mainProgram endsOutput complete
💡 Program ends after printing outerVal accessed by inner class
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
outerValN/A"Hello from Outer""Hello from Outer""Hello from Outer""Hello from Outer"
Outer instanceN/ACreatedCreatedCreatedCreated
Inner instanceN/AN/ACreatedCreatedCreated
Key Moments - 2 Insights
How does the inner class access the outer class's member without creating a new instance?
The inner class holds a reference to its outer class instance, so it can directly access outerVal as shown in step 3 of the execution_table.
Why do we need to create the inner class instance from an outer class instance?
Because the inner class is tied to an outer instance, you must create it via Outer().Inner() as shown in step 2, ensuring it can access outer members.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of outerVal after step 1?
AUndefined
Bnull
C"Hello from Outer"
D"Hello from Inner"
💡 Hint
Check the 'Result' column in row for step 1 in execution_table.
At which step is the inner class instance created?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table to find when Inner instance is created.
If we tried to create Inner instance without Outer instance, what would happen?
AIt would work fine
BCompilation error because Inner needs Outer instance
CRuntime error only
DInner class would create its own Outer instance automatically
💡 Hint
Recall key_moments about how Inner class is tied to Outer instance.
Concept Snapshot
Inner classes in Kotlin can access outer class members directly.
Create inner instance via Outer().Inner() to link them.
Inner class holds reference to outer instance.
Use inner class methods to access outer members.
This allows clean encapsulation and access.
Full Transcript
This example shows how an inner class in Kotlin can access members of its outer class. First, an Outer class instance is created with a member outerVal. Then, an Inner class instance is created from the Outer instance. The inner class method printOuter accesses outerVal directly and prints its value. The execution table traces these steps, showing variable states and actions. Key points include that the inner class holds a reference to the outer instance, so it can access outerVal without creating a new Outer. The inner instance must be created from an existing Outer instance. The visual quiz tests understanding of these steps and relationships.