October 2019
Intermediate to advanced
434 pages
11h 54m
English
Similarly to properties, we can apply @JvmStatic to methods defined on companion objects or named objects. Let's examine this by adding a method to our existing companion object:
class Widget { companion object { ... fun goo() { } }}
From Java, we can then use this method by once again using an instance of the companion object:
// Main.javapublic class Main { public static void main(String[] args) { Widget.Companion.goo(); }}
In this example, we must reference the companion object instance because the goo() method is a member of the companion object itself, as seen in the following generated code:
public final class Widget { ... public static final class Companion { ... public final void goo() { } ... }}
To move this generated ...
Read now
Unlock full access