JvmStatic, JvmOverloads, and JvmField in Kotlin - GeeksforGeeks

19 Sep, 2021
In this article, we’ll look at how to utilize Kotlin code in our current Java code using @JvmStatic, @JvmOverloads, and @JvmField in Kotlin. You may invoke Kotlin code from Java code and vice versa. This blog will concentrate on invoking Kotlin code from Java code.
Kotlin
Java
// Kotlin Classclass GeeksforGeeks// Calling from Kotlinval article = Article()
// Class in Java being calledArticle geeksforgeeks = new Article();
As you can see in the preceding code, calling the Kotlin code from the Java code is fairly straightforward, but there are times when it is not. In those instances, we utilize @JvmStatic, @JvmOverloads, and @JvmField in Kotlin to make it simple for someone calling the Kotlin code from Java.
Package-level functions are represented as static methods in Kotlin. You can also use the @JvmStatic annotation in Kotlin to create static methods for functions specified in a companion object or named object. As an example:
Kotlin
object GeeksforGeeks {    fun useSomeLogic() {        // your code goes here    }}
Let’s call Kotlin now:
Kotlin
GeeksforGeeks.useSomeLogic()
You’ll need to make a call like this from Java.
Java
GeeksforGeeks.INSTANCE.useSomeLogic();
How can we make it function if we don’t use the INSTANCE?
JvmStatic is the answer.
Kotlin
object GeeksforGeeks {    @JvmStatic    fun useSomeLogic() {        // logic goes here    }}
Java
GeeksforGeeks.useSomeLogic();
If you do not supply a date in the argument, the current date will be used by default in the above code. So, if we call from Kotlin, the following code will execute without error:
Kotlin
val articleOne = Event("GeeksforGeeks")val articleTwo = Event("Android", Date())
However, if we call the same function from Java, we must supply all of the arguments, or we would receive the following error:
Java
Article articleOne = new Event("GeeksforGeeks");Article articleTwo = new Event("GeeksforGeeks", new Date());
We’ve passed both arguments. So, what’s the point of using the default value if we have to pass both values?
So, we can use the @JvmOverloads annotation to utilize the default value. Following the use of the annotation, the Kotlin code will be:
data class GeeksforGeeks @JvmOverloads constructor( val articleName: String, val date: Date = Date())
Let’s use the Event class from the previous section of the article as an example.
data class GeeksforGeeks ( val articleName: String, val date: Date = Date())
So, if you want a certain field to be utilized as a regular field rather than a getter or setter, you must instruct the compiler not to produce any getter or setter for it, which you can accomplish by using the @JvmField annotation. So, after applying the @JvmField annotation, the Kotlin code will be:
data class Article (@JvmField val geeksforgeeks: String, val date: Date = Date())
Now, in Java, you can access the class’s fields in the same manner as you can in Kotlin:
Java
Article newArticle = new Event("GeeksforGeeks", new Date());String articleName = newArticle.name;
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Retrofit with Kotlin Coroutine in Android
How to Post Data to API using Retrofit in Android?
Android UI Layouts
Kotlin Array
Retrofit with Kotlin Coroutine in Android
How to Get Current Location in Android?
Kotlin Setters and Getters