JvmStatic Annotation in Kotlin
- Authors
- Name
- Amit Shekhar
- Published on
The JvmStatic annotation in Kotlin lets the Java code call a function of a named object or companion object directly, like a static method, without using INSTANCE.
In this blog, we are going to learn about the JvmStatic annotation in Kotlin.
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning, and Android at Outcome School.
Join Outcome School and get a high-paying tech job:
Let's get started.
The best thing about Kotlin is that it is designed with Java interoperability in mind. It means that the existing Java code can be called from Kotlin, and also the Kotlin code can be called from Java. Both ways are supported.
Today, we will focus on calling the Kotlin code from Java as we want to learn about the JvmStatic annotation.
The best way to learn this is by taking an example.
Assume that we have a named object AppUtils in Kotlin as below:
object AppUtils {
fun install() {
}
}
We can call the install() method in Kotlin as below:
AppUtils.install()
But, when we call install() method from Java as below:
AppUtils.install(); // compilation error
We get the compilation error.
We will have to call it as below:
AppUtils.INSTANCE.install(); // works
This works as expected.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
So, the question is: Can we make it work without using that INSTANCE?
The answer is yes. By using the JvmStatic annotation, we can make it work without using that INSTANCE.
For that, we need to update our named object AppUtils in Kotlin as below:
object AppUtils {
@JvmStatic
fun install() {
}
}
Here, we have added @JvmStatic annotation on the method. So, the compiler will generate an additional static method for us.
Now, we can call install() method from Java as below:
AppUtils.install();
And, it works perfectly.
This is how we can use the JvmStatic annotation in Kotlin.
To learn Kotlin in depth covering Java/Kotlin interop, JVM annotations like JvmStatic, JvmField, JvmOverloads, companion object, and Kotlin language internals, we have a complete Android Program by Outcome School.
Learn about JvmOverloads: JvmOverloads Annotation in Kotlin
Learn about JvmField: JvmField Annotation in Kotlin
Watch the video format: JvmStatic Annotation in Kotlin
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
Frequently Asked Questions
Why do we need INSTANCE to call a Kotlin object method from Java?
Without @JvmStatic, the method of a named object is not static for Java. So, calling AppUtils.install() from Java gives a compilation error. We have to call it as AppUtils.INSTANCE.install() instead. This works, but it does not look the same as the Kotlin call.
What does the compiler generate when we use @JvmStatic?
The compiler generates an additional static method for us. Because of that static method, Java can call the method directly on the object name, like AppUtils.install(), without using INSTANCE. This makes the Java call look the same as the Kotlin call.
Do we need @JvmStatic to call an object method from Kotlin?
No. In Kotlin, we can already call the method as AppUtils.install() without any annotation. The @JvmStatic annotation is needed only when we want Java to call it the same way, without INSTANCE.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
