Java interoperability is one of the key advantages of Kotlin. It helped easier transitions to Kotlin while keeping part of your code in Java.

Why?

Jvm Annotations inform the Kotlin compiler to generate method’s or field’s against the default kotlin compiler behaviour. This promotes easy interoperability with exisiting Java classes or libraries.

Jvm Annotations

Here are some of the commonly used JVM annotations in Kotlin while developing an Android app:

  1. @JvmStatic – Generates static code to access companion properties, methods directly.
  2. @JvmField – Generates class fields with public access.
  3. @JvmOverloads – Generates multiple overload’s for a function with default value params.
  4. @JvmName – Generates a alternate named class, getter or setter.

Your own libraries, modules or 3rd party code written in Java can smoothly access your Kotlin code with the help of above annotations.

One thought on “Kotlin’s JVM annotations”

Comments are closed.