JvmField Annotation in Kotlin

Authors
  • Amit Shekhar
    Name
    Amit Shekhar
    Published on
JvmField 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 JvmField 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 JvmField 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 object and get the name in Kotlin as below:

val session = Session("Session", Date())
val name = session.name

It works as expected.

But, when we create the object and get the name from Java as below:

Session session = new Session("Session", new Date());
String name = session.name; // compilation error

It will not compile. From Java, we will have to use the getter method as below:

Session session = new Session("Session", new Date());
String name = session.getName();

Now, it will compile and work as expected.

So, the question is: Can we use it without the getter method as we use in Kotlin?

The answer is yes. By using the JvmField annotation. So, if we want a field to be used as a normal field and not as a getter or setter then we will have to tell the compiler not to generate any getter and setter for the same and expose it as a field by using the @JvmField annotation.

Let's update our data class Session as below:

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

Notice that we have used @JvmField over the field name to instruct the Kotlin compiler not to generate any getter and setter for the same and expose it as a field.

Now, if we create the object and get the name from Java as below:

Session session = new Session("Session", new Date());
String name = session.name;

Now, it works perfectly. It will compile as expected as the Kotlin compiler will not generate any getter and setter for the same and expose it as a field.

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

Watch the video format: JvmField Annotation in Kotlin

Learn about JvmStatic: JvmStatic Annotation in Kotlin

Learn about JvmOverloads: JvmOverloads 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.