Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin static functions : companion object, @JvmStatic @JvmField

I have just started messing around with the Kotlin programming language, which is pretty much cooler than Java. I have some doubts related to static methods and fields:

Q1: Official document says

Kotlin can also generate static methods for functions defined in named objects or companion objects if you annotate those functions as @JvmStatic.

But if you see below I can access bar() method as a static method, which works without using @JvmStatic annotation. But on official doc its throwing error -> Kotlin static method.

Class C{
    companion object{
        @JvmStatic
        fun foo() { }
        fun bar();
    }
}
fun main(args: Array<String>) {
    C.foo();
    C.bar(); //this line works fine
}

Q2: Do I really need @JvmStatic and @JvmField to make things static? As you can see with companion object, things are working as expected.

like image 924
Bharatesh Avatar asked Jan 12 '18 11:01

Bharatesh


People also ask

Is companion object static in Kotlin?

companion object is how you define static variables/methods in Kotlin. You are not supposed to create a new instance of Retrofit / ApiService each time you execute a request, however.

What is use of @jvmstatic in Kotlin?

Put simply, this annotation tells the Kotlin compiler to generate one additional static method for the annotated function under the hood. Moreover, the most important use case for this annotation is, of course, better Java interoperability.

What is the difference between companion object and object in Kotlin?

Object expressions are executed (and initialized) immediately, where they are used. Object declarations are initialized lazily, when accessed for the first time. A companion object is initialized when the corresponding class is loaded (resolved) that matches the semantics of a Java static initializer.

What does Jvmstatic mean?

(Common source) (JVM source) Specifies that an additional static method needs to be generated from this element if it's a function. If this element is a property, additional static getter/setter methods should be generated.


1 Answers

You can access members of a companion object as C.bar() in Kotlin, but not in Java. Without @JvmStatic, you would need to use C.Companion.bar() in Java, just as said in the docs.

Note that, without @JvmStatic, the function is compiled to an instance (non-static) method that is called on C.Companion in Java (and Kotlin simply shortens it to a call on C, but it's the same under the hood), so yes, you need either @JvmStatic and @JvmField to make a declaration in a companion object compile into a static member.

Basically, @JvmStatic and @JvmField are tools for Java interoperation that help with creating Java-friendly APIs, and if you don't need to call the Kotlin members from Java (e.g. they are internal to your Kotlin project, or you are developing a library that is unlikely to be used with Java), you can leave them as they are.

like image 195
hotkey Avatar answered Oct 29 '22 00:10

hotkey