January 2018
Intermediate to advanced
434 pages
14h 1m
English
Similar to extension properties, we can have companion object extensions, which means we can add methods to the companion object of a class that helps us access it in a static way. Let's look at an example. Suppose we have a Student class:
class Student(val age:Int){ companion object{ }}
Let's now add an extension method to the companion object:
fun Student.Companion.sayHi(){ println("Hi")}
Now you can access it on the class without creating an instance of the class:
Student.sayHi()
Read now
Unlock full access