Problem Statement
Explain the difference between nested classes and inner classes in Kotlin.
Explanation
Nested classes in Kotlin are declared inside another class but are static by default, meaning they don't hold a reference to the outer class instance and cannot access outer class members directly. They're useful for grouping related classes together without creating unnecessary coupling, like defining a Builder class inside the class it builds.
Inner classes marked with the inner keyword do hold a reference to the outer class instance accessed with this at OuterClass syntax, allowing them to access outer class members including private ones. Inner classes are necessary when the nested class needs to interact with the outer class state, common in callback implementations or adapter patterns.
This default behavior is opposite to Java where nested classes are non-static by default potentially causing memory leaks if the nested class outlives the outer class, while Kotlin's static-by-default nested classes prevent accidental memory leaks. Only use inner when you genuinely need access to the outer class, otherwise prefer nested classes for better encapsulation and avoiding unintended references.