0
0
Kotlinprogramming~10 mins

Nested class independence from outer in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested class independence from outer
Define Outer Class
Define Nested Class inside Outer
Create Nested Class Instance
Nested Class Accesses Own Members
No Access to Outer Class Members
Nested Class Works Independently
Shows how a nested class is defined inside an outer class but works independently without accessing outer class members.
Execution Sample
Kotlin
class Outer {
    val outerVal = "Outer"
    class Nested {
        fun nestedFun() = "Nested"
    }
}

val nested = Outer.Nested()
println(nested.nestedFun())
Defines an outer class with a nested class inside; nested class method returns its own string, showing independence.
Execution Table
StepActionEvaluationResult
1Define class Outer with property outerValouterVal = "Outer"Outer class ready
2Define nested class Nested inside OuterNested class has method nestedFun()Nested class ready
3Create instance of Nested: Outer.Nested()nested = Outer.Nested()Nested instance created
4Call nested.nestedFun()nestedFun() returns "Nested"Output: Nested
5Try to access outerVal from Nested (not shown in code)Not allowed without referenceNo access to outerVal
6Program endsExecution complete
💡 Nested class instance created and method called; nested class does not access outer class members.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
outerVal"Outer""Outer""Outer""Outer"
nestednullInstance of NestedInstance of NestedInstance of Nested
Key Moments - 2 Insights
Why can't the nested class access the outer class's property directly?
Because the nested class is static by default in Kotlin and does not hold a reference to the outer class instance, as shown in step 5 of the execution_table.
How do we create an instance of the nested class?
We create it using Outer.Nested(), not through an instance of Outer, as shown in step 3 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when nested.nestedFun() is called at step 4?
A"Outer"
B"Nested"
CError
Dnull
💡 Hint
Check the 'Result' column at step 4 in the execution_table.
At which step is the nested class instance created?
AStep 3
BStep 1
CStep 5
DStep 6
💡 Hint
Look at the 'Action' column for instance creation in the execution_table.
If the nested class tried to access outerVal directly, what would happen according to the execution_table?
AIt would access outerVal successfully
BIt would return null
CIt would cause a compile error
DIt would print "Outer"
💡 Hint
Refer to step 5 in the execution_table about access to outerVal.
Concept Snapshot
Nested classes in Kotlin are static by default.
They do not have access to outer class instance members.
Create nested class instances with Outer.Nested().
Nested class methods work independently.
To access outer members, use inner classes instead.
Full Transcript
This example shows a Kotlin outer class with a nested class inside. The nested class has its own method returning "Nested". We create an instance of the nested class using Outer.Nested() and call its method. The nested class cannot access the outer class's property directly because it is static by default. The execution table traces these steps clearly, showing the nested class's independence from the outer class instance.