> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin Nested Class

A nested class in Kotlin is a class declared within the lexical scope of another class. By default, nested classes are statically bound, meaning they do not hold an implicit reference to an instance of their enclosing (outer) class. This behavior is structurally equivalent to a `static` nested class in Java.

Because a nested class lacks a reference to the outer class instance, it cannot access the instance members (properties or functions) of the enclosing class. It operates entirely independently of the outer class's state.

## Syntax and Instantiation

To instantiate a nested class, you qualify the nested class name with the outer class name, treating the outer class purely as a namespace. You do not need to instantiate the outer class first.

```kotlin theme={"dark"}
class Outer {
    private val outerInstanceProperty: String = "Outer State"

    class Nested {
        fun printMessage() {
            println("Executing inside the nested class.")
            
            // The following line would cause a compilation error:
            // println(outerInstanceProperty) 
        }
    }
}

// Instantiation requires the Outer class name as a qualifier, 
// but does NOT require an instance of Outer.
val nestedObject = Outer.Nested()
nestedObject.printMessage()
```

## Nesting Interfaces

Kotlin allows interfaces to be nested within classes, and classes to be nested within interfaces. By definition, all nested interfaces and classes nested within interfaces are implicitly static.

```kotlin theme={"dark"}
class Outer {
    interface NestedInterface {
        fun execute()
    }
}

// Implementing a nested interface
class Implementation : Outer.NestedInterface {
    override fun execute() {
        println("Implemented nested interface")
    }
}
```

## Contrast with Inner Classes

To alter the default static binding of a nested class so that it *can* access the outer class's instance members, the nested class must be explicitly modified with the `inner` keyword.

Marking a class as `inner` changes its memory footprint and instantiation mechanics. It forces the nested class to hold a reference to the outer class instance, meaning the nested class can no longer be instantiated without first creating an instance of the outer class.

```kotlin theme={"dark"}
class Outer {
    private val outerState: String = "Outer State"

    // 'inner' keyword changes the class from statically nested to an inner class
    inner class Inner {
        fun accessOuter() {
            // Access to outer class members is now permitted
            println(outerState) 
        }
    }
}

// Instantiation now requires an instance of the Outer class
val outerInstance = Outer()
val innerObject = outerInstance.Inner()
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
