liveBook · Manning

@JvmStatic

As explained in Kotlin in Action, 2E

@JvmStatic is a Kotlin annotation that causes a companion-object or object member to be exposed as a static method to Java code.

@JvmStatic makes a member of a companion object or object available to Java as a static method. Kotlin normally compiles a companion object as a separate object, so Java may otherwise call the member through Companion or a named companion object. section 4.4.3

Expose a companion member to Java

class Person(val name: String) { companion object { @JvmStatic fun fromJSON(jsonText: String): Person = /* ... */ } } Java can call the annotated member as a static method: Person.fromJSON("..."); Without the annotation, the generated access uses the companion object instead: Person.Companion.fromJSON("..."); section 4.4.3

Use @JvmStatic for Java-facing APIs when Java callers require familiar static syntax. For a static field, use @JvmField on a top-level property or an object property instead. section 4.4.3

How Kotlin in Action, 2E covers it

How deep it goes
Explains companion-object compilation and Java call syntax, but does not detail annotation implementation internals.
Examples
Uses Person.fromJSON and contrasts it with Java's Person.Companion.fromJSON call.
What it assumes
Assumes companion objects and basic Java calls, while showing how Kotlin members otherwise appear through Companion.
What you can do after
Apply @JvmStatic to expose Kotlin members through Java-compatible static call syntax.
How it connects
Connects companion objects, Java interoperability, static methods, and @JvmField.

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.