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?
The companion objects can access private members of the class. Hence, they can be used to implement the factory method patterns.
A companion object is an object that's declared in the same file as a class , and has the same name as the class. A companion object and its class can access each other's private members. A companion object's apply method lets you create new instances of a class without using the new keyword.
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.
Object expressions are executed (and initialized) immediately, where they are used. Object declarations are initialized lazily, when accessed for the first time. A companion object is initialized when the corresponding class is loaded (resolved) that matches the semantics of a Java static initializer.
Like a singleton, but to access the singleton object you have to initialize the "outer" class first. We can say that companion is same as "Static Block" like Java, But in case of Kotlin there is no Static Block concept, than companion comes into the frame. class Example { companion object { fun display () { //place your code } } }
However, in Kotlin, you can also call callMe () method by using the class name, i.e, Person in this case. For that, you need to create a companion object by marking object declaration with companion keyword. When you run the program, the output will be:
Package and class naming rules in Kotlin are quite simple: Names of packages are always lowercase and do not use underscores ( org.example.project ). Using multi-word names is generally discouraged, but if you do need to use multiple words, you can either just concatenate them together or use camel case ( org.example.myProject ).
So..what does this keyword do? In short, companion objects are singleton objects whose properties and functions are tied to a class but not to the instance of that class — basically like the “static” keyword in Java but with a twist.
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
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() }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With