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?