Bird
Raised Fist0
Kotlinprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In Kotlin, what keyword must you use to allow an inner class to access members of its outer class?
easy
A. inner
B. outer
C. this
D. nested

Solution

  1. Step 1: Understand inner class behavior

    By default, nested classes in Kotlin cannot access outer class members directly.
  2. Step 2: Identify the keyword for access

    The keyword inner allows the nested class to hold a reference to the outer class and access its members.
  3. Final Answer:

    inner -> Option A
  4. Quick Check:

    Use inner for access [OK]
Hint: Remember: only 'inner' classes access outer members directly [OK]
Common Mistakes:
  • Confusing 'inner' with 'nested' keyword
  • Using 'this' instead of 'inner'
  • Assuming all nested classes access outer members
2. Which of the following Kotlin class declarations correctly allows the inner class to access the outer class's property name?
easy
A. class Outer { val name = \"Kotlin\"; inner class Inner { fun printName() = println(name) } }
B. class Outer { val name = \"Kotlin\"; class Inner { fun printName() = println(name) } }
C. class Outer { val name = \"Kotlin\"; nested class Inner { fun printName() = println(name) } }
D. class Outer { val name = \"Kotlin\"; inner class Inner { fun printName() = println(this.name) } }

Solution

  1. Step 1: Check class declaration syntax

    Only the class declared with inner keyword can access outer class properties directly.
  2. Step 2: Analyze each option

    class Outer { val name = \"Kotlin\"; inner class Inner { fun printName() = println(name) } } uses inner and accesses name correctly. class Outer { val name = \"Kotlin\"; class Inner { fun printName() = println(name) } } lacks inner. class Outer { val name = \"Kotlin\"; nested class Inner { fun printName() = println(name) } } uses invalid nested keyword. class Outer { val name = \"Kotlin\"; inner class Inner { fun printName() = println(this.name) } } tries to access this.name which refers to inner class, not outer.
  3. Final Answer:

    B -> Option A
  4. Quick Check:

    Use inner and direct property name [OK]
Hint: Use 'inner' keyword and access outer property by name [OK]
Common Mistakes:
  • Omitting 'inner' keyword
  • Using 'nested' instead of 'inner'
  • Using 'this.name' inside inner class incorrectly
3. What is the output of the following Kotlin code?
class Outer(val x: Int) {
    inner class Inner {
        fun printX() = println(x)
    }
}

fun main() {
    val outer = Outer(10)
    val inner = outer.Inner()
    inner.printX()
}
medium
A. Runtime exception
B. 0
C. Compilation error
D. 10

Solution

  1. Step 1: Understand inner class access

    The inner class can access the outer class property x because it is declared with inner.
  2. Step 2: Trace the code execution

    An Outer object is created with x=10. The inner class instance calls printX() which prints x value 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Inner class prints outer property value [OK]
Hint: Inner class prints outer property directly if 'inner' keyword used [OK]
Common Mistakes:
  • Assuming inner class cannot access outer property
  • Expecting 0 instead of 10
  • Thinking code causes error
4. Identify the error in this Kotlin code snippet:
class Outer {
    val message = "Hello"
    class Inner {
        fun greet() = println(message)
    }
}
medium
A. Incorrect println syntax
B. Missing semicolon after 'message' declaration
C. Inner class cannot access 'message' without 'inner' keyword
D. Outer class must be declared as 'inner'

Solution

  1. Step 1: Check nested class access rules

    Nested classes without inner keyword cannot access outer class properties.
  2. Step 2: Identify cause of error

    The Inner class tries to print message but cannot see it, causing a compilation error.
  3. Final Answer:

    Inner class cannot access 'message' without 'inner' keyword -> Option C
  4. Quick Check:

    Use 'inner' to access outer members [OK]
Hint: Add 'inner' keyword to nested class to fix access error [OK]
Common Mistakes:
  • Thinking semicolon is required in Kotlin
  • Assuming println syntax is wrong
  • Confusing outer class declaration with inner
5. Given this Kotlin code, what will be the output?
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))
}
hard
A. 53
B. 15
C. Compilation error due to access
D. Runtime exception

Solution

  1. Step 1: Understand inner class access and method

    The inner class can access the outer class property number because it is declared with inner. The method multiplies number by the given factor.
  2. Step 2: Calculate the output

    Outer number is 5, factor is 3, so 5 * 3 = 15 is printed.
  3. Final Answer:

    15 -> Option B
  4. Quick Check:

    Inner class method uses outer property correctly [OK]
Hint: Inner class can use outer properties in calculations [OK]
Common Mistakes:
  • Concatenating instead of multiplying
  • Expecting access error without 'inner'
  • Confusing output with string '53'
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard