patternjavaCritical
What is the equivalent of Java static methods in Kotlin?
Viewed 0 times
kotlinjavamethodstheequivalentstaticwhat
Problem
There is no
What is the best way to represent a
static keyword in Kotlin.What is the best way to represent a
static Java method in Kotlin?Solution
You place the function in the "companion object".
So the java code like this:
will become
You can then use it from inside Kotlin code as
But from within Java code, you would need to call it as
(Which also works from within Kotlin.)
If you don't like having to specify the
From the docs:
Companion Objects
An object declaration inside a class can be marked with the
keyword:
Members of the companion object can be called by using simply the class
name as the qualifier:
...
However, on the JVM you can have members of companion objects generated
as real static methods and fields, if you use the
annotation. See the Java interoperability section for more details.
Adding the
and then it will exist as a real Java static function, accessible from
both Java and Kotlin as
If it is just disliked for the
provide an explicit name for the companion object looks like this:
which will let you call it from Kotlin in the same way, but
from java like
So the java code like this:
class Foo {
public static int a() { return 1; }
}will become
class Foo {
companion object {
fun a() : Int = 1
}
}You can then use it from inside Kotlin code as
Foo.a();But from within Java code, you would need to call it as
Foo.Companion.a();(Which also works from within Kotlin.)
If you don't like having to specify the
Companion bit you can either add a @JvmStatic annotation or name your companion class.From the docs:
Companion Objects
An object declaration inside a class can be marked with the
companionkeyword:
class MyClass {
companion object Factory {
fun create(): MyClass = MyClass()
}
}Members of the companion object can be called by using simply the class
name as the qualifier:
val instance = MyClass.create()...
However, on the JVM you can have members of companion objects generated
as real static methods and fields, if you use the
@JvmStaticannotation. See the Java interoperability section for more details.
Adding the
@JvmStatic annotation looks like thisclass Foo {
companion object {
@JvmStatic
fun a() : Int = 1;
}
}and then it will exist as a real Java static function, accessible from
both Java and Kotlin as
Foo.a().If it is just disliked for the
Companion name, then you can alsoprovide an explicit name for the companion object looks like this:
class Foo {
companion object Blah {
fun a() : Int = 1;
}
}which will let you call it from Kotlin in the same way, but
from java like
Foo.Blah.a() (which will also work in Kotlin).Code Snippets
class Foo {
public static int a() { return 1; }
}class Foo {
companion object {
fun a() : Int = 1
}
}Foo.Companion.a();class MyClass {
companion object Factory {
fun create(): MyClass = MyClass()
}
}val instance = MyClass.create()Context
Stack Overflow Q#40352684, score: 1560
Revisions (0)
No revisions yet.