JvmStatic, JvmOverloads, and JvmField in Kotlin

Banner Jvmstatic Jvmoverloads And Jvmfield In Kotlin

In this blog, we will learn how we can use the Kotlin code in our existing Java code with the help of @JvmStatic, @JvmOverloads, and @JvmField in Kotlin.

You can call the Kotlin code from your Java code and vice-versa. For this blog, we will focus on calling Kotlin code from Java code.

// This is a class in Kotlin
class User

// Calling from Kotlin
val user = User()

// Calling from Java
User user = new User();

As you can see in the above code, it is very simple to call the Kotlin code from the Java code, but there are cases when it is not that simple, in those cases we use @JvmStatic, @JvmOverloads, and @JvmField in Kotlin to make it simple for someone who is calling the Kotlin code from Java.

@JvmStatic

In Kotlin, the package-level functions are represented as static methods. Also, in Kotlin, you can make static methods for functions that are defined in some companion object or named object by using the @JvmStatic annotation. For example:

// Kotlin
object Utils {
    fun doSomething() {
        // logic here
    }
}

Now, calling from Kotlin

// calling from Kotlin class
Utils.doSomething()

But, when you call this from Java, you get a compilation error.

// calling from Java class
Utils.doSomething(); // compilation error

You will have to call like this from Java.

// calling from Java class
Utils.INSTANCE.doSomething(); // works fine

How can we make it work without using the INSTANCE?

The answer is JvmStatic.

Our updated Kotlin code:

// Kotlin
object Utils {

    @JvmStatic
    fun doSomething() {
        // logic here
    }
}

Notice that we have added JvmStatic annotation.

Now, we can simply call it from our Java code like below:

// calling from Java class
Utils.doSomething(); // works fine

This is how JvmStatic annotation is useful.

@JvmOverloads

Let’s take an example of a data class Event.

data class Event( val name: String, val date: Date = Date())

Here, in the above code, by default, the current date will be taken if you are not passing the date in the parameter. So, if we are calling from Kotlin, the following code will run with no error:

val eventOne = Event("MindOrks")
val eventTwo = Event("MindOrks", Date())

But if we are calling the same from the Java then you have to pass all the parameters otherwise you will get some error:

Event eventOne = new Event("MindOrks");//Here we are passing only one argument so we will encounter error
Event eventTwo = new Event("MindOrks", new Date());//No error

We have pass both the parameters. So, what's the use of default value if we have to pass both values.

So, to use the default value, we can use the @JvmOverloads annotation. Now, after using the annotation, the Kotlin code will be:

data class Event @JvmOverloads constructor( val name: String, val date: Date = Date())

Here, we have used the @JvmOverloads annotation. Now, calling from Java, you do not need to pass all the parameters:

Event eventOne = new Event("MindOrks"); //No error
Event eventTwo = new Event("MindOrks", new Date()); //No error

@JvmField

Let's take the same example of the Event class that we saw in the above part of the blog.

data class Event ( val name: String, val date: Date = Date())

So, if we are accessing the properties of the class then the code in Kotlin is:

val event = Event("MindOrks", Date())
val name = event.name

But in Java, you have to use the getter method:

Event event = new Event("MindOrks", new Date());
String name = event.getName();

So, if you want a particular field to be used as a normal field and not as getter or setter then you have to tell the compiler not to generate any getter and setter for the same and this can be done by using the @JvmField annotation. So, the Kotlin code after using the @JvmField annotation will be:

data class Event (@JvmField val name: String, val date: Date = Date())

Now, in Java also, you can access the fields of the class in the same way as in Kotlin:

Event event = new Event("MindOrks", new Date());
String name = event.name;

That’s all for this blog. Hope you enjoyed this blog.

To learn more about Android, you can visit our blogging website.

Keep Learning :)

Team MindOrks!