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.