
Build 10x products in minutes by chatting with AI - beyond just a prototype.
Are there static methods in Kotlin?
Why doesn't Kotlin have static?
What is a static variable in Kotlin?
Kotlin, unlike Java, does not have the static keyword traditionally used to declare static methods or properties. This fundamental difference in design leads to unique patterns for achieving similar functionality.
In this blog, you will learn about Kotlin static methods, companion objects, and various strategies to define static-like behavior in Kotlin.
In Kotlin, static methods refer to methods that can be called without needing an instance of a class. This functionality is typically achieved through companion objects, package-level functions, or by declaring methods in a Kotlin object. Each of these approaches has its advantages and appropriate use cases.
The primary mechanism for simulating static methods in Kotlin is through companion objects. When you declare a companion object within a class, you can define functions that behave like static methods in Java.
For example, consider the following Kotlin code:
class MyClass { companion object { fun staticMethod(): String { return "Hello from staticMethod" } } } fun main() { println(MyClass.staticMethod()) // Output: Hello from staticMethod }
In this example, staticMethod can be called directly on MyClass without creating an instance, similar to how a static method works in Java.
To enhance interoperability with Java, Kotlin provides the @JvmStatic annotation. This annotation allows you to access companion object methods as if they were static methods in Java.
Here’s how you can implement this:
class MyClass { companion object { @JvmStatic fun staticMethod(): String { return "Hello from @JvmStatic method" } } } fun main() { println(MyClass.staticMethod()) // Can also be called from Java as MyClass.staticMethod() }
Adding @JvmStatic generates a static method in the compiled Java class, making it more straightforward to call from Java code.
Another idiomatic way to define static methods in Kotlin is through package-level functions. These functions exist outside any class and can be called without needing an instance of a class.
Consider the following example:
package utilities fun staticFunction(): String { return "Hello from a package-level function" } // In main.kt import utilities.staticFunction fun main() { println(staticFunction()) // Output: Hello from a package-level function }
Kotlin compiles these functions into static methods of a generated class named after the file (e.g., UtilitiesKt).
Kotlin's object declarations allow you to create singleton instances that can contain static-like methods and properties. This method is useful for grouping related functions together.
Here’s how it works:
object MySingleton { fun staticMethod(): String { return "Hello from MySingleton" } } fun main() { println(MySingleton.staticMethod()) // Output: Hello from MySingleton }
This pattern effectively encapsulates functionality in a single instance without the overhead of creating multiple instances.
While Kotlin does not support static nested classes as in Java, you can achieve similar functionality by using companion objects or defining nested classes within a companion object. This approach keeps related classes logically grouped without requiring an instance of the enclosing class.
Kotlin static methods is fundamentally different from Java's, offering cleaner and more idiomatic alternatives. By leveraging companion objects, package-level functions, and Kotlin objects, you can define static-like methods efficiently while maintaining code clarity and reducing boilerplate. As you dive deeper into Kotlin, understanding these concepts will significantly enhance your programming capabilities.
class OuterClass {
companion object Nested {
class StaticNestedClass {
fun display() = "Hello from Static Nested Class"
}
}
}
fun main() {
val nested = OuterClass.Nested.StaticNestedClass()
println(nested.display()) // Output: Hello from Static Nested Class
}