JvmOverloads Annotation in Kotlin

Authors
  • Amit Shekhar
    Name
    Amit Shekhar
    Published on
JvmOverloads Annotation in Kotlin

I am Amit Shekhar, Co-Founder @ Outcome School, 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.

Join Outcome School and get high paying tech job: Outcome School

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

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

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())

We can create the objects in Kotlin as below:

val sessionOne = Session("Session One", Date())
val sessionTwo = Session("Session Two")

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

The first line will compile, but the second line will not as we have not passed Date 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)

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 or constructor 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())

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

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

Session sessionOne = new Session("Session One", new Date());
Session sessionTwo = new Session("Session Two");

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

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

Watch the video format: JvmOverloads Annotation in Kotlin

Learn about JvmStatic: JvmStatic Annotation in Kotlin

Learn about JvmField: JvmField Annotation in Kotlin

Prepare yourself for Android Interview: Android Interview Questions

That's it for now.

Thanks

Amit Shekhar
Co-Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.