October 2019
Intermediate to advanced
434 pages
11h 54m
English
Let's examine the impact of @JvmStatic on a companion object property. Let's start out with the following snippet, which defines a companion object on a Widget class:
class Widget { companion object { val foo = "foo" }}
The foo property we defined earlier can be used from Java, as shown in the following code:
// Main.javapublic class Main { public static void main(String[] args) { Widget.Companion.getFoo(); }}
There is a possible issue with this code, though. The foo property is an instance on an actual class. This means that an instance of the companion object has to be instantiated to access foo:
public final class Widget { @NotNull private static final String foo = "foo"; ... public static final class Companion ...
Read now
Unlock full access