June 2018
Intermediate to advanced
310 pages
6h 32m
English
Since everything in Java is an object (unless you're a primitive type), we're used to putting all the constants inside our objects as static members.
And since Kotlin has companion objects, we usually try putting them there:
class MyClass { companion object { val MY_CONST = "My Const" }}
This will work, but you should remember that companion object is an object, after all.
So, this will be translated to the following code, more or less:
public final class Spock { @NotNull private static final String MY_CONST = "My Const"; public static final Spock.Companion Companion = new Spock.Companion(...); public static final class Companion { @NotNull public final String getMY_CONST() { return MyClass.MY_CONST; } private Companion() { } } ...
Read now
Unlock full access