https://kotlinlang.org logo
Title
m

mugur

06/12/2017, 1:57 PM
Is there a way to have a static method in Kotlin?
I tried Class1{ companion object Class2{ fun somestaticMethod()}}, but I have to call it with Class1.Class2.somestaticMethod()
d

david.bilik

06/12/2017, 1:59 PM
so skip Class2 name
Class1{ companion object { fun somestaticMethod()}}
e

egor

06/12/2017, 2:23 PM
don’t forget that you can define a
fun
by itself – there’s no need to surround it by a
class
s

sksk

06/12/2017, 2:27 PM
and
@JvmStatic
if you wan it to be accessible from java
👍 1
r

radityagumay

06/12/2017, 11:00 PM
Add function inside companion object, all fun inside that will regard as final, you should add annotation processor @JvmStatic. This annotation tells jvm you function or field is static.
👍 1