liveBook · Manning

@JvmField

As explained in Kotlin in Action, 2E

@JvmField is a Kotlin annotation that exposes a property as a Java field instead of only through accessors.

@JvmField exposes a Kotlin property as a public Java field, making it useful when Java code requires field-based access rather than Kotlin-generated accessors. section 4.4.3 section 12.1.2

Expose an object property as a Java field

object BuildConfig { @JvmField val version = "1.0" } /* Java */ String version = BuildConfig.version; section 4.4.3 section 12.1.2

Apply the annotation to a top-level property or a property declared in an object when the Java-facing API must contain a field. Unlike @JvmStatic, which exposes methods as static Java methods, @JvmField changes property visibility for Java callers. section 4.4.3 section 12.1.2

Use @JvmField when these conditions apply:

  • Java integration requires direct field access. section 4.4.3
  • The declaration is a top-level property or belongs to an object. section 4.4.3
  • The Java API should provide no getter or setter for the property. section 12.1.2

How Kotlin in Action, 2E covers it

How deep it goes
Defines placement and Java visibility, but defers detailed annotation rules and bytecode mechanics.
Examples
Uses an object property accessed directly from Java as a field.
What it assumes
Assumes familiarity with Kotlin properties, objects, Java callers, and annotations.
What you can do after
Apply it via Kotlin's @JvmField annotation to expose selected properties directly to Java callers.
How it connects
Connects properties with Java interoperability, object declarations, companion objects, annotations, and @JvmStatic.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.