Michael Anderson
Michael Anderson

Reputation: 73480

What is the point of naming a companion object in kotlin

The documentation for companion objects has the following example

class MyClass {
    companion object Factory {
        fun create(): MyClass = MyClass()
    }
}

Here Factory is the name of the companion object. It then goes on to say:

The name of the companion object can be omitted, in which case the name Companion will be used:

However there is no example that I can see that uses the name of the companion object.

Since you can only have one companion object per class (otherwise you get a Only one companion object is allowed per class error) the name feels like some pretty useless syntactic sugar to me.

What can the name of the companion object actually be used for? Why would one bother to use any name for it?

Upvotes: 31

Views: 8047

Answers (4)

Hardik Trivedi
Hardik Trivedi

Reputation: 6092

Well, companion objects in Kotlin are not just syntactic sugar. They are actually a type. They are able to do much more thing, and need not to be see as just replacement of static.

You can actually extend class or implement an interface. See an example below.

open class Super {
    open fun sayHello() {
        println("Hello")
    }
}

class Some {
    companion object Child : Super() {
        override fun sayHello() {
            super.sayHello()
            println("Hello from companion object")
        }
    }
}

fun main() {
    Some.Child.sayHello()
}

Upvotes: 5

Human
Human

Reputation: 10815

However there is no example that I can see that uses the name of the companion object.

class Person(val name: String) { companion object Loader {
fun fromJSON(jsonText: String): Person = ... }
}
>>> person = Person.Loader.fromJSON("{name: 'Dmitry'}") >>> person.name
Dmitry
>>> person2 = Person.fromJSON("{name: 'Brent'}") >>> person2.name
Brent

Upvotes: 1

guenhter
guenhter

Reputation: 12167

You can use the name of the companion like:

MyClass.create()           // not via companion name
MyClass.Companion.create() // via default companion name
MyClass.Factory.create()   // via companion name

The name is maybe not that important for Kotlin, because you can just access the method without knowing that there is a companion object (line one above). It is more like a personal style, if you want to make the access to such functions more explicit.

But for java interop it makes a difference, because you have to access the function via the companion name:

    MyClass.Factory.create();   // with named companion
    MyClass.Companion.create(); // with unnamed comanion

Upvotes: 18

s1m0nw1
s1m0nw1

Reputation: 81899

If you do not use an explicit name, the companions name is Companion, thus it can be omitted, like you already quoted.

Sometimes you may want to have an explicit name in your calls, which would be MyClass.Factory.create() in your example. For namespace reasons maybe.

I don't see a many reasons to name a companion object, either. Except if you care about Java interop with your Kotlin code. Then, you need to explicitly write the companions name.

Another reason you might care about the name is, when you define an extension function on it:

  fun MyClass.Companion.ext() = "myext"

In this case, it can be clearer when it has a name like Factory, on which specific factory methods are added via extension.

Upvotes: 2

Related Questions