Static method in Kotlin

Find this useful? Support us: Star on GitHub 6
Category: Class | Language: Kotlin

Sure, I'd be happy to explain how to define a static method in a class in Kotlin.

In Kotlin, we use the companion object to define static methods, just like in Java. Here is an example of how to define a static method in Kotlin:

class MyClass {
    companion object {
        // static method
        fun myStaticMethod() {
            println("This is a static method in Kotlin!")
        }
    }
}

In the code above, we have defined a MyClass class and a companion object containing a static method called myStaticMethod(). This method can be called on the class itself, rather than an instance of that class.

To call the static method, we simply use the class name followed by the method name, like this:

MyClass.myStaticMethod() // output: This is a static method in Kotlin!

That's all there is to defining a static method in Kotlin using a companion object! It is worth noting that companion objects can also hold static properties, just like in Java.