@JvmOverloads in Kotlin
10 Oct 2017@JvmStatic
and @JvmField
are well-known annotations that can be used in Kotlin files to expose companion object methods and constants. But the other day I had an “oh wow” moment with @JvmOverloads
so now it has joined the pantheon of great JVM annotations provied by Kotlin. Consider this method in a network utility written in Kotlin:
From a Kotlin file you can call this method in many ways:
But from Java you’re a little more restricted. Without the @JvmOverloads
annotation from a Java file you would need to call the method with all three parameters: makeNetworkCall("www.mark.gg", 4, true)
. Adding @JvmOverloads generates two overloaded methods for you using the default values specified in the function declaration.
![](/assets/posts/2017/10-10/jvm-overloads.png)
New programming languages like Swift and Kotlin can (naïvely) be evaluted solely on the merits of the language itself but a more accurate evaluation will look at the ecosystem: Xcode, Objective-C, Swift and Android Studio, Java, Kotlin. Annotations like @JvmOverloads
make Kotlin a team player in an already established ecosystem giving it much more potential for disrupting and improving that ecosystem.