August 2017
Intermediate to advanced
440 pages
10h
English
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 ...
Read now
Unlock full access