DEV Community

Cover image for JvmOverloads Annotation in Kotlin
Amit Shekhar
Amit Shekhar

Posted on • Edited on • Originally published at outcomeschool.com

JvmOverloads Annotation in Kotlin

Hi, I am Amit Shekhar, Co-Founder @ Outcome School • IIT 2010-14 • I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

In this blog, we are going to learn about the JvmOverloads annotation in Kotlin.

This article was originally published at Outcome School.

The best thing about Kotlin is that it is designed with Java interoperability in mind. It means that the existing Java code can be called from Kotlin, and also the Kotlin code can be called from Java. Both ways are supported.

Today, we will focus on calling the Kotlin code from Java as we want to learn about the JvmOverloads annotation.

The best way to learn this is by taking an example.

Assume that we have a data class Session in Kotlin as below:

data class Session(val name: String, val date: Date = Date())
Enter fullscreen mode Exit fullscreen mode

We can create the objects in Kotlin as below:

val sessionOne = Session("Session One", Date())
val sessionTwo = Session("Session Two")
Enter fullscreen mode Exit fullscreen mode

Both the lines of the code will compile as expected as for the 2nd object, it will take the default value of the Date.

But, when we create the objects from Java as below:

Session sessionOne = new Session("Session One", new Date());
Session sessionTwo = new Session("Session Two"); // compilation error
Enter fullscreen mode Exit fullscreen mode

The first line will compile, but the second line will not as we have not passed Data as a parameter. It will give us the following error:

Session(java.lang.String, java.util.Date)' in 'com.example.Session' cannot be applied to '(java.lang.String)
Enter fullscreen mode Exit fullscreen mode

As we know that Java does not support default parameters. It supports overloading.

And, Kotlin supports default parameters.

So, we will have to use @JvmOverloads. It instructs the Kotlin compiler to generate overloads for the function that substitute default parameter values.

Let's update our data class Session as below:

data class Session @JvmOverloads constructor(val name: String, val date: Date = Date())
Enter fullscreen mode Exit fullscreen mode

Notice that we have used @JvmOverloads to instruct the Kotlin compiler to generate overloads for the function.

Now, if we create the objects from Java as below:

Session sessionOne = new Session("Session One", new Date());
Session sessionTwo = new Session("Session Two");
Enter fullscreen mode Exit fullscreen mode

And, it works perfectly. Both the lines of the code will compile as expected as the Kotlin compiler has generated the overloads for the function.

This is how we can use the JvmOverloads annotation in Kotlin.

That's it for now.

Thanks

Amit Shekhar

Co-Founder @ Outcome School

You can connect with me on:

Read all of our blogs here.

Top comments (0)