The bottom line is, despite their differences at the language level, static initializer blocks and companion object initializers are the same at the bytecode level. Using a companion object, we can make one function or property of a class available on class level. ![]() Companion object in Kotlin : Companion object is similar to static properties and methods of Java. ![]() In this post, I will show you where JvmStatic is used and its use cases. Only after that will the JVM execute the initializer logic. JvmStatic annotation or JvmStatic is an important annotation of Kotlin. Kotlin creators are extensively using it, for example in Coroutines - so I guess we should too. ![]() I tried to show interesting ways of using companion object which is not just a container for constants, or is emulating the static keyword from Java. In the kotlin language, static is one of the main and default keyword variables and functions for implementing the kotlin application. Public final class ĥ: invokespecial #40 // Method Static$Companion."":(LDefaultConstructorMarker )VĨ: putstatic #42 // Field Companion:LStatic$Companion ġ3: putstatic #20 // Field answer:LString Ģ1: getstatic #52 // Field System.out:LPrintStream Ģ5: invokevirtual #58 // Method PrintStream.println:(LObject )Vīefore executing the logic in the init block, the JVM creates an instance of the companion class (index 0 to 5), and stores it as a static member inside the enclosing class (index 8). Static Factory Methods are often used in Kotlin itself or by 3rd party library creators. In this article, we extended the in-memory store sample.We found out how to reeive a value from an instance’s and static field of Kotlin in native code, how to call Kotlin methods.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |