Sitemap

JVM Static Annotation explained simply

3 min readAug 17, 2025
Press enter or click to view image in full size
Photo by Marc Reichelt on Unsplash

Kotlin is designed for seamless interoperability with Java, but the two languages handle static members differently. The @JvmStatic annotation is a key tool for bridging this gap, making Kotlin code more accessible and idiomatic when called from Java.

What is @JvmStatic?

The @JvmStatic annotation tells the Kotlin compiler to generate an additional static method for the annotated function or property when compiling to JVM bytecode. This means that, although Kotlin does not have static members in the same way as Java, you can expose functions and properties as true static members for Java consumers.

How Does It Work?

In Kotlin, you typically define static-like members inside a companion object or an object declaration. By default, these are accessed via the companion or object instance from Java. The compiler generates a static method directly on the containing class, allowing Java code to access it just like any other Java static method.

Example:

Kotlin Code:

Let's try using it in Java.

It can not be accessed in a Kotlin manner. We need to add a companion to access the logIt function. How can we avoid it?

By using a JVM static annotation.

Why Use @JvmStatic?

  • Java Interoperability: The main reason is to make Kotlin code more idiomatic and convenient for Java users. Many Java frameworks and libraries, such as JUnit, expect static methods for certain APIs (e.g., test providers, utility methods).
  • Cleaner Java Syntax: Java developers can call the method directly on the class, without needing to reference the companion or singleton instance.
  • Framework Compatibility: Some Java frameworks require static methods for callbacks, configuration, or reflection. @JvmStatic ensures your Kotlin code fits these expectations.
  • Readability: It makes the Java-side usage of your Kotlin utilities more readable and familiar to Java developers

When Should You Use @JvmStatic?

  • When exposing utility functions or constants from Kotlin to Java.
  • When working with Java frameworks that require static methods (e.g., JUnit, Android APIs).
  • When you want to provide a familiar static API for Java consumers.

Best Practices

  • Use @JvmStatic Only when you need Java interoperability. For pure Kotlin code, prefer idiomatic Kotlin constructs.
  • Avoid overusing it — only annotate functions or properties that truly need to be static from Java’s perspective.
  • For constants, consider @JvmField instead if you want to expose a field without getters/setters.

Conclusion

The @JvmStatic annotation is essential for writing Kotlin code that integrates smoothly with Java, especially when static methods are expected.

If you want to learn more. Here is a video for you:

--

--

Hitesh Kohli
Hitesh Kohli

Written by Hitesh Kohli

Hi, my name is Hitesh Kohli, I work at Geeks for Geeks as an Android developer. I love messing around with apps and games.

No responses yet