What is the equivalent of Java static methods in Kotlin?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Java, static methods are a common feature used in object-oriented programming. They belong to the class, rather than any instance of the class, allowing them to be called without creating an object of the class. Kotlin handles static methods differently, primarily due to its emphasis on top-level functions and its companion object feature.
Understanding Static Methods in Java
In Java, static methods are declared using the static keyword. These methods can be accessed directly using the class name and do not require an instance of the class. Here’s a simple example:
To use this method, you would simply call it like so:
Kotlin’s Approach to Static Methods
Kotlin does not have a direct equivalent to Java's static methods. Instead, Kotlin offers several alternatives, each suitable for different scenarios:
- Companion Objects
- Object Declarations
- Package-level Functions
- @JvmStatic Annotation
1. Companion Objects
The most common and direct equivalent to Java’s static methods in Kotlin is using companion objects. You can define methods within a companion object that acts similarly to static methods.
To call this method, you use the containing class name followed by the method name:
2. Object Declarations
For cases where you don't necessarily need a class but just a singleton object with static-like methods, Kotlin's object declaration can be used.
You can access the static-like method as follows:
3. Package-level Functions
If the function does not use any properties of the class, it can be placed directly in a file outside of any class. This is suitable for utility functions.
This function can be called from anywhere in the package:
4. @JvmStatic Annotation
When interoperating with Java code, Kotlin provides the @JvmStatic annotation to make a function in an object or companion object available as a Java static method.
In Java, this can be called as:
Table of Kotlin Static Method Equivalents
| Feature | Use Case |
| Companion Object | Methods relating to the class but not instances. |
| Object Declaration | Singleton utility-like methods independent of any class. |
| Package-level Functions | Standalone functions that don’t need class context. |
| @JvmStatic Annotation | Interoperability with Java requiring static methods. |
Conclusion
Kotlin provides several tools that can act as equivalents to Java's static methods. The choice among these tools should depend on the specific case—like whether you need class association, package-level utility, or Java interoperability. Understanding these distinctions and capabilities allows Kotlin developers to write more idiomatic and effective Kotlin code.

