October 2019
Intermediate to advanced
434 pages
11h 54m
English
Nested classes in Kotlin behave a bit differently than in Java. A class defined within another class is not treated as an inner class by default, meaning it does not have an implicit reference to the enclosing class. In the following example, the Nested class cannot access Outer.a because it is not declared as an inner class using the inner keyword:
class Outer { val a = "a" val b = "b" class Nested { val c = a // error }}
If we look at the decompiled bytecode, we see that Nested is a static class, and therefore cannot access members of the enclosing class:
public final class Outer { @NotNull private final String a = "a"; @NotNull private final String b = "b"; ... public static final class Nested { @NotNull private final
Read now
Unlock full access