https://kotlinlang.org logo
Title
e

evkaky

05/10/2017, 3:57 PM
guys, I need to emulate java static method in my kotlin code. I tried to use compain object, but seems it’s a wrong choice:
class MyClass {
	companion object {
		fun myCoolStaticMethod() {

		}
	}
}
Java library (junit 5), which expects existing of static method myCoolStaticMethod tell me, that there is no such method in MyClass What I’am doing wrong?
k

kevinmost

05/10/2017, 3:58 PM
evkaky: it only gets exported as an actual static function if you annotate the function with
@JvmStatic
e

evkaky

05/10/2017, 3:59 PM
wow, thank you, my kotlin-friend
👍 1
j

j_kapp

05/10/2017, 4:12 PM
Kotlin doesn't support static methods, this should only be used to make Kotlin code work with Java code.
k

kevinmost

05/10/2017, 4:13 PM
Yes, by doing this you lose out on some of the nice things that you can do because the functions are non-static. Namely, that you can have method inheritance and that they can be implemented on your companion object off a superclass or interface
it's definitely required when you have Java libraries that are expecting a static method to be there though, like with JUnit