Companion object in Kotlin

In Kotlin or any other programming language like Java and C#, whenever we want to call the method or whenever we want to access the members of a class then we make the object of the class and with the help of that object, we access the members of the class. A typical Kotlin example of the same is:

class ToBeCalled {
    fun callMe() = println("You are calling me :)")
}
fun main(args: Array<String>) {     
    val obj = ToBeCalled()
    
    // calling callMe() method using object obj
    obj.callMe()
}

In the above example, we are calling the callMe() method of the ToBeCalled class by creating one object of the ToBeCalled class and then with the help of that object, the callMe() method is called.

The static keyword

In some language like Java and C#, we use static keyword to declare the members of the class and use them without making any object i.e. just call them with the help of class name. So, to call a method named myMethod() and the class name is MyClass, then the method will be called by:

MyClass.myMethod();

But can we do the same i.e. call the members of the class with the help of class name in Kotlin? The answer is yes, but the approach is different. There is nothing called static in Kotlin. So, in Kotlin, we use a companion object. Let’s see how can we achieve this.

Companion object

In Kotlin, if you want to write a function or any member of the class that can be called without having the instance of the class then you can write the same as a member of a companion object inside the class. So, by declaring the companion object, you can access the members of the class by class name only.

To create a companion object, you need to add the companion keyword in front of the object declaration.

class CompanionClass {

    companion object CompanionObject {

    }
}
val obj = CompanionClass.CompanionObject

Also, we can remove the companion object name and in place of the companion object name, we can use the keyword Companion i.e. the default name of the companion object will be Companion, like below:

class CompanionClass {

    companion object {

    }
}
val obj = CompanionClass.Companion

So, following is the example of companion object in Kotlin:

class ToBeCalled {
    companion object Test {
        fun callMe() = println("You are calling me :)")
    }
}
fun main(args: Array<String>) {
    ToBeCalled.callMe()
}

The output of the above code is “ You are calling me :) ”

To have a clear understanding of the same, let’s look at the example of Singleton class where we are having a single instance of the class.

class SingletonExample {

    private constructor() {
    }

    companion object {
        private lateinit var instance: SingletonExample

        val myInstance: SingletonExample
            get() {
                if (instance == null) {
                    instance = SingletonExample()
                }

                return instance
            }
    }

    fun someMethod(variableName: String): Boolean {
        Log.d("Method called", variableName)
        return true;
    }
}

To use the someMethod() method, write the below code:

SingletonExample.Companion.myInstance.someMethod("Some string")

If you are having some name to your companion object then you can call the someMethod() by:

SingletonExample.CompanionObjectName.myInstance.someMethod("Some string")

So, hope you liked the blog and will use the concept of companion in your Android application. To know more about some of the cool topics of Android, you can visit our blogging website.

Keep Learning :)

Team MindOrks!