Currently, when we convert a Java code into Kotlin, it pollutes all Java files that use the static functions.
This means that if I have this in Java:
public class Foo {
public static void foo() {
Log.d("AppLog", "Hello");
}
public void foo2() {
Log.d("AppLog", "World");
}
}
public class Foo2 {
public void foo() {
Foo.foo();
}
}
And I convert the file of class “Foo”, I get this instead:
class Foo {
fun foo2() {
Log.d("AppLog", "World")
}
companion object {
fun foo() {
Log.d("AppLog", "Hello")
}
}
}
public class Foo2 {
public void foo() {
Foo.Companion.foo();
}
}
So it changed the Java class file too, and it would have changed all other usages of this static function to be with Companion.
, polluting them and causing un-needed clutter.
On the other hand, we can add @JvmStatic
to the function on Kotlin, to avoid this and not force the Java code to change.
However, this requires that we add it to every static function, and by definition, it shouldn’t even be needed, because behind the scenes, even though Java “thinks” it’s a static function, it’s not:
Note that, even though the members of companion objects look like static members in other languages, at runtime those are still instance members of real objects, and can, for example, implement interfaces:
Source: https://kotlinlang.org/docs/reference/object-declarations.html
So my request:
Remove all need for @JvmStatic
in the first place.
Make it work the same for all languages. Let Java access it like on Kotlin, even if it looks like it’s something else.
This removes clutter of both @JvmStatic
in Kotlin files and Companion.
in Java files.
It also doesn’t force us to change the code of both Kotlin and Java.
No effort, and doesn’t cost anything.