October 2019
Intermediate to advanced
434 pages
11h 54m
English
When a companion object is declared within a Kotlin class, the compiler will generate an inner class named Companion during compilation. Here, we've defined an empty companion object on a Widget class:
class Widget { companion object { }}
From the preceding code snippet, the following Java code is generated by the compiler:
public final class Widget { public static final Widget.Companion Companion = new Widget.Companion((DefaultConstructorMarker)null); ... public static final class Companion { private Companion() { } ... }}
As you can see here, any time an instance of the enclosing Widget class is instantiated, an associated instance of the inner Companion class will also be instantiated. From Java, properties ...
Read now
Unlock full access