Inner class access to outer members in Kotlin - Time & Space Complexity
Let's see how the time it takes to run code changes when an inner class accesses members of its outer class.
We want to know how the number of steps grows as the input size changes.
Analyze the time complexity of the following code snippet.
class Outer(val numbers: List<Int>) {
inner class Inner {
fun sum(): Int {
var total = 0
for (num in numbers) {
total += num
}
return total
}
}
}
fun main() {
val outer = Outer(listOf(1, 2, 3, 4, 5))
val inner = outer.Inner()
println(inner.sum())
}
This code defines an outer class holding a list of numbers and an inner class that sums those numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of numbers inside the inner class's sum() function.
- How many times: Once for each number in the list (n times).
As the list gets bigger, the sum function takes longer because it adds each number one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The time grows directly with the number of items; doubling the list doubles the work.
Time Complexity: O(n)
This means the time to run the sum grows in a straight line with the size of the list.
[X] Wrong: "Accessing outer class members from the inner class adds extra loops or slows down the code significantly."
[OK] Correct: Accessing outer members is direct and does not add extra loops; the main time cost is still just looping through the list once.
Understanding how inner classes access outer members helps you explain code structure and performance clearly, a useful skill in many coding discussions.
What if the inner class stored a copy of the list instead of accessing the outer class's list directly? How would the time complexity change?
Practice
Solution
Step 1: Understand inner class behavior
By default, nested classes in Kotlin cannot access outer class members directly.Step 2: Identify the keyword for access
The keywordinnerallows the nested class to hold a reference to the outer class and access its members.Final Answer:
inner -> Option AQuick Check:
Useinnerfor access [OK]
- Confusing 'inner' with 'nested' keyword
- Using 'this' instead of 'inner'
- Assuming all nested classes access outer members
name?Solution
Step 1: Check class declaration syntax
Only the class declared withinnerkeyword can access outer class properties directly.Step 2: Analyze each option
class Outer { val name = \"Kotlin\"; inner class Inner { fun printName() = println(name) } } usesinnerand accessesnamecorrectly. class Outer { val name = \"Kotlin\"; class Inner { fun printName() = println(name) } } lacksinner. class Outer { val name = \"Kotlin\"; nested class Inner { fun printName() = println(name) } } uses invalidnestedkeyword. class Outer { val name = \"Kotlin\"; inner class Inner { fun printName() = println(this.name) } } tries to accessthis.namewhich refers to inner class, not outer.Final Answer:
B -> Option AQuick Check:
Useinnerand direct property name [OK]
- Omitting 'inner' keyword
- Using 'nested' instead of 'inner'
- Using 'this.name' inside inner class incorrectly
class Outer(val x: Int) {
inner class Inner {
fun printX() = println(x)
}
}
fun main() {
val outer = Outer(10)
val inner = outer.Inner()
inner.printX()
}Solution
Step 1: Understand inner class access
The inner class can access the outer class propertyxbecause it is declared withinner.Step 2: Trace the code execution
AnOuterobject is created withx=10. The inner class instance callsprintX()which printsxvalue 10.Final Answer:
10 -> Option DQuick Check:
Inner class prints outer property value [OK]
- Assuming inner class cannot access outer property
- Expecting 0 instead of 10
- Thinking code causes error
class Outer {
val message = "Hello"
class Inner {
fun greet() = println(message)
}
}Solution
Step 1: Check nested class access rules
Nested classes withoutinnerkeyword cannot access outer class properties.Step 2: Identify cause of error
TheInnerclass tries to printmessagebut cannot see it, causing a compilation error.Final Answer:
Inner class cannot access 'message' without 'inner' keyword -> Option CQuick Check:
Use 'inner' to access outer members [OK]
- Thinking semicolon is required in Kotlin
- Assuming println syntax is wrong
- Confusing outer class declaration with inner
class Outer(val number: Int) {
inner class Inner {
fun multiply(factor: Int) = number * factor
}
}
fun main() {
val outer = Outer(5)
val inner = outer.Inner()
println(inner.multiply(3))
}Solution
Step 1: Understand inner class access and method
The inner class can access the outer class propertynumberbecause it is declared withinner. The method multipliesnumberby the given factor.Step 2: Calculate the output
Outer number is 5, factor is 3, so 5 * 3 = 15 is printed.Final Answer:
15 -> Option BQuick Check:
Inner class method uses outer property correctly [OK]
- Concatenating instead of multiplying
- Expecting access error without 'inner'
- Confusing output with string '53'
Ready to prove it?
Pass the test and this pattern moves unknown
