An inner class is declared inside another class and retains a reference to an instance of the outer class. Kotlin keeps nested classes independent by default; add inner when outer-instance access is required. section 4.1.4 section 4.6
Making outer access explicit
class Outer { inner class Inner { fun getOuterReference(): Outer = this@Outer } } Here, inner makes Inner retain an Outer reference, and this@Outer identifies the enclosing instance. Without inner, Kotlin's nested class resembles Java's static nested class. section 4.1.4
Serializable view state
interface State : Serializable class Button : View { override fun getCurrentState(): State = ButtonState() override fun restoreState(state: State) { /* ... */ } class ButtonState : State { /* ... */ } } ButtonState is deliberately nested rather than inner, so it does not capture Button implicitly. In Java, the equivalent class is inner by default; that hidden reference can cause serialization to fail when Button is not serializable. section 4.1.4
Table 4.3 Correspondence between nested and inner classes in Java and Kotlin (view table figure)
Use the correspondence table to compare Java's static and ordinary nested-class forms with Kotlin's nested and inner modifiers. section 4.1.4
