Java
Kotlin
Programming
Static Methods
Code Conversion

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:

java
1public class Utils {
2    public static int add(int a, int b) {
3        return a + b;
4    }
5}

To use this method, you would simply call it like so:

java
int sum = Utils.add(5, 3);

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:

  1. Companion Objects
  2. Object Declarations
  3. Package-level Functions
  4. @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.

kotlin
1class Utils {
2    companion object {
3        fun add(a: Int, b: Int): Int {
4            return a + b
5        }
6    }
7}

To call this method, you use the containing class name followed by the method name:

kotlin
val sum = Utils.add(5, 3)

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.

kotlin
1object MathUtils {
2    fun multiply(a: Int, b: Int): Int {
3        return a * b
4    }
5}

You can access the static-like method as follows:

kotlin
val product = MathUtils.multiply(4, 2)

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.

kotlin
fun subtract(a: Int, b: Int): Int {
    return a - b
}

This function can be called from anywhere in the package:

kotlin
val difference = subtract(10, 6)

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.

kotlin
1class Utils {
2    companion object {
3        @JvmStatic
4        fun multiply(a: Int, b: Int): Int {
5            return a * b
6        }
7    }
8}

In Java, this can be called as:

java
int product = Utils.multiply(4, 2);

Table of Kotlin Static Method Equivalents

FeatureUse Case
Companion ObjectMethods relating to the class but not instances.
Object DeclarationSingleton utility-like methods independent of any class.
Package-level FunctionsStandalone functions that don’t need class context.
@JvmStatic AnnotationInteroperability 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.


Course illustration
Course illustration

All Rights Reserved.