@JvmStatic cannot be used to create static interface methods?

I primarily upgraded to Kotlin 1.1 so that my Kotlin code would no longer have to be crippled by long-since obsolete Java 6 limitations. (In Kotlin 1.0, I’m strangely forced to write many files in Java in order to accomplish things.)

Is there some design reason that the following code is not supported in Kotlin 1.1-M04, or is it just not implemented yet in this EAP release?

interface Foo
{
    companion object 
    {
        @JvmStatic
        fun foo() = Unit
    }
}

Only functions in named objects and companion objects of classes can be annotated with '@JvmStatic'

http://try.kotlinlang.org/#/UserProjects/l6jliabi2q476e81ub44cp278/od9up8pb7pqcsm92t0tkq0ldgl

Even more importantly though, why is the -jvm-target option locked to 1.6 in the IDEA plugin??

Note that using the command line to specify the 1.8 target level doesn’t change things:

>kotlinc-jvm.bat -jvm-target 1.8 static_interface_method.kt
static_interface_method.kt:6:9: error: only functions in named objects and companion objects of classes can be annotated with '@JvmStatic'
        @JvmStatic
        ^
1 Like

Support for generating Java 1.8 bytecode was reverted in Kotlin 1.1-M04, and it’s unclear whether it will reappear in the final release of Kotlin 1.1. Any additional features that depend on it, such as support for @JvmStatic methods in interfaces, will definitely be implemented post-1.1.

Ouch.

That’s greatly disappointing, but I at least appreciate the prompt response Dmitry.

At least 1.1 isn’t without some other improvements…

I know this is an old topic, but whatever happened with this issue? Is -jvm-target 1.8 still a no-op in 1.1.3? I’m still getting calls to static methods in Java interfaces are deprecated in JVM target 1.6. Recompile with '-jvm-target 1.8' even with that flag set, which is confusing if it’s been removed.

2 Likes

I’ve just hit this. I’m using Kotlin 1.2.10 and jvmTarget 1.8. Is there any way to generate a static method on a Kotlin interface?

This is something we do a lot and if it’s not possible then our interfaces will have to be defined in Java which would be a shame.

While generating jvm-1.8-specific bytecode is currently supported by Kotlin and is even required in some cases, that feature of declaring static members in interfaces is not supported at this moment.
It is tracked here: https://youtrack.jetbrains.com/issue/KT-6301. You can vote for it there and even better you can add your use cases for that feature, if they are not already listed there.

1 Like