Companion objects

An object declaration inside a class can be specified as a companion object of it, just like in the following code:

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

This means that methods of object declaration inside can be invoked by just specifying the name of the outer class, as follows:

var bean = Bean.create();

A companion object is initialized when the outer class enclosing it is loaded similarly to a static initializer in Java.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.172.252