Nested class independence from outer in Kotlin - Time & Space Complexity
We want to understand how the time it takes to run code changes when using nested classes in Kotlin.
Does the nested class depend on the outer class for its speed?
Analyze the time complexity of the following code snippet.
class Outer {
class Nested {
fun countTo(n: Int) {
for (i in 1..n) {
println(i)
}
}
}
}
fun main() {
val nested = Outer.Nested()
nested.countTo(5)
}
This code defines a nested class inside an outer class and runs a simple loop inside the nested class method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop inside the
countTomethod that prints numbers from 1 to n. - How many times: It runs exactly n times, where n is the input number.
As the input number n grows, the loop runs more times, increasing the work linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loops |
| 100 | 100 loops |
| 1000 | 1000 loops |
Pattern observation: The number of operations grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to run the method grows in a straight line with the input size.
[X] Wrong: "The nested class slows down the code because it depends on the outer class."
[OK] Correct: The nested class here is independent and runs its own code. It does not add extra loops or slow down the process.
Understanding how nested classes work helps you explain code structure clearly and shows you know how different parts of code affect performance.
"What if the nested class accessed a list from the outer class and looped over it? How would the time complexity change?"
Practice
nested class in Kotlin compared to an inner class?Solution
Step 1: Understand nested class independence
In Kotlin, a nested class is static by default and cannot access outer class members.Step 2: Compare with inner class
Only inner classes (declared withinner) can access outer class properties and functions.Final Answer:
A nested class does not have access to the outer class's members. -> Option BQuick Check:
Nested class independence = A [OK]
- Confusing nested and inner classes
- Thinking nested classes access outer members
- Assuming nested classes require 'inner' keyword
Solution
Step 1: Recall Kotlin nested class syntax
In Kotlin, nested classes are declared simply inside the outer class without any keyword.Step 2: Identify incorrect options
innerdeclares an inner class,staticandnestedkeywords do not exist in Kotlin.Final Answer:
class Outer { class Nested { } } -> Option DQuick Check:
Nested class syntax = B [OK]
- Using 'static' keyword (Java style)
- Using 'nested' keyword (not valid Kotlin)
- Confusing inner and nested class syntax
class Outer {
val x = 10
class Nested {
fun printX() = "Cannot access x"
}
}
fun main() {
val nested = Outer.Nested()
println(nested.printX())
}Solution
Step 1: Analyze nested class access
The nested class cannot access the outer class propertyxbecause it is not an inner class.Step 2: Check printX() function
It returns the string "Cannot access x" explicitly, so that will be printed.Final Answer:
Cannot access x -> Option AQuick Check:
Nested class method output = D [OK]
- Expecting nested class to print 10
- Assuming compilation error due to access
- Confusing inner and nested class behavior
class Outer {
val y = 5
class Nested {
fun printY() = y
}
}Solution
Step 1: Check nested class access rules
A nested class cannot access outer class properties directly because it is static by default.Step 2: Understand how to fix
To access outer properties, the class must be declared asinner, but here it is not.Final Answer:
Nested class cannot access 'y' directly; must be inner class. -> Option AQuick Check:
Nested class access error = A [OK]
- Thinking 'inner' keyword is optional
- Assuming 'y' can be static in Kotlin
- Ignoring nested class static nature
Nested to access outerValue from Outer class?class Outer(val outerValue: Int) {
class Nested {
fun printValue() = "Cannot access outerValue"
}
}Solution
Step 1: Understand nested vs inner class access
Nested classes cannot access outer class properties unless declaredinner.Step 2: Apply inner class to access outerValue
Changing Nested toinner class Nestedallows direct access toouterValue.Final Answer:
Change Nested to an inner class and access outerValue directly. -> Option CQuick Check:
Use inner class for outer access = C [OK]
- Trying to make properties static (not Kotlin style)
- Passing outerValue manually instead of inner class
- Using companion object incorrectly for instance data
Ready to prove it?
Pass the test and this pattern moves unknown
