JvmField Annotation in Kotlin
Tags: kotlin, android
Published: 2022-09-28 06:00:28

---
title: JvmField Annotation in Kotlin
published: true
description: In this blog, we are going to learn about the JvmField annotation in Kotlin.
tags: kotlin, android
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lja9ioza075e8ps8sud6.png
canonical_url: https://amitshekhar.me/blog/jvmfield-annotation-in-kotlin
---

I am [**Amit Shekhar**](https://amitshekhar.me), a mentor helping developers in getting high-paying tech jobs.

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

**This article was originally published at [amitshekhar.me](https://amitshekhar.me/blog/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:

```kotlin
data class Session(val name: String, val date: Date = Date())
```

We can create the object and get the `name` in Kotlin as below:

```kotlin
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:

```java
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:

```java 
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:

```kotlin 
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:

```java 
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.

That's it for now.

Thanks

[**Amit Shekhar**](https://amitshekhar.me)

You can connect with me on:

- [Twitter](https://twitter.com/amitiitbhu)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)
- [Facebook](https://www.facebook.com/amit.shekhar.iitbhu)

[**Read all of my high-quality blogs here.**](https://amitshekhar.me/blog)