Is there a way to have a static method in Kotlin?
# announcements
m
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
so skip Class2 name
Class1{ companion object { fun somestaticMethod()}}
e
don’t forget that you can define a
fun
by itself – there’s no need to surround it by a
class
s
and
@JvmStatic
if you wan it to be accessible from java
👍 1
r
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