You're reading for free via Android Dev Nexus' Friend Link. Become a member to access the best of Medium.

Member-only story

@JvmField Annotation in Kotlin

Simplifying Interoperability with Java — Part 2

Android Dev Nexus
3 min readOct 12, 2024

Here’s a special treat for our non-Medium members: we’re offering free access to this blog! Just follow this link — because your curiosity and passion for coding should never be limited! 😉😉

👻 Hello dear Kotlin witches and warlocks! 🎃

As the moon rises and the shadows grow longer, it’s time to brew up another magical potion from the depths of Kotlin’s spellbook. Today, we summon the powers of @JvmField! 🧙‍♀️ This little-known incantation can turn your Kotlin properties into fields that even Java can access without chanting "getter" or "setter" spells.

Ready to conjure up some field magic? Let’s begin… 👻

Example with Data Class and @JvmField

Let’s summon a Kotlin data class that we want to expose to Java. Normally, Kotlin automatically generates getter and setter methods for each property.

data class User(
var name: String = "Agatha",
val age: Int = 550
)

When this Kotlin code is compiled to Java bytecode, it generates:

public class User {
private String name = "Agatha"; // Backing field for `name`
private final int age = 550; // Backing field for `age`

// Getter for `name`
public String getName() {
return this.name;
}

// Setter for `name`
public void setName(String name) {
this.name = name;
}

// Getter for `age`
public int getAge() {
return this.age;
}
}
Kotlin property compiled to getter, setter (var properties) and a private field with the same name as property (properties with backing fields) in Java

Thus in Java, we would have to access these properties via generated methods, not the private field:

// Java code
User user = new User();

System.out.println(user.name); // compile error

// Accessing via getter
System.out.println(user.getName());

// Setting via setter
user.setName("Wanda");

Is there any way to do this without getters and setters — just like traditional public fields in Java?

With @JvmField

data class User(
@JvmField var name: String = "Agatha",
val age: Int = 550
)

Now in java, we can access these fields without using getter and setter methods.

// Java code
User user = new User();

// Accessing directly
System.out.println(user.name);

// Setting directly
user.name = "Wanda";

💡Here compiler will not generate getter and setter methods for the field.

Are there any cases where we shouldn’t use @JvmField?

Like any good spell, you should use @JvmField wisely. Here are some scenarios where you might want to avoid it:

  1. Custom Getters/Setters — Since @JvmField disables the automatic generation of these methods.
  2. Maintain control over access @JvmField exposes the property as a public field in Java, breaking encapsulation.
  3. No Lateinit or Delegated Properties

Here’s the Part 1 of our series.

⭐ Got any topic requests? We’re all ears! 👂 Feel free to send us topic requests at droid.dev.nexus@gmail.com or hit us up in the comments — we post new blogs daily! 🧑🏻‍💻 ⭐ ️

Found this helpful? Give it a spooky clap 👏 or brew us a potion here! 🧙‍♂️🍵 And don’t forget to summon your favourite Kotlin topics in the comments below! 🎃🕸️👻

Until then Happy coding! 💻🎉

References:

Kotlin Official Documentation

Android Dev Nexus
Android Dev Nexus

Written by Android Dev Nexus

Your friendly neighborhood developers working at PayPal, turning Android fundamentals into concepts you’ll actually understand.

Responses (2)

What are your thoughts?

Spoookyyy Post

All of a sudden, I'm scared of JVM field 😛