Nested classes
A nested class is a class defined inside another class. Nesting small classes within top-level classes places the code closer to where it is used, and allows a better way of grouping classes. Typical examples are Tree
/Leaf
listeners or presenter states. Kotlin in a similar way to Java, allows us to define a nested class, and there are two main ways to do this. We can define a class as a member inside a class:
class Outer { private val bar: Int = 1 class Nested { fun foo() = 2 } } val demo = Outer.Nested().foo() // == 2
The preceding example allows us to create an instance of the Nested
class without creating instances of the Outer
class. In this case, a class cannot refer directly to instance variables or methods defined in its enclosing class (it can use them only through an object reference). This is equivalent to a Java static nested class and, in general, static members.
To be able to access the members of an outer...