Object-oriented programming is a model of programming language that is based on objects that can represent data. Kotlin supports object-oriented programming in the same way that Java does, but even more strictly. This is because Kotlin doesn't have primitive types and static members. Instead, it provides a companion object:
class Bar {
companion object {
const val NAME = "Igor"
fun printName() = println(NAME)
}
}
The companion object is an object that is created once, during class initialization. In Kotlin, we can refer to members of companion object in the same way as static in Java:
fun test() {
Bar.NAME
Bar.printName()
}
However, under the hood, the nested Companion class is created, and we actually use an instance of this class, as follows:
Bar.Companion.printName();
Moreover, Kotlin supports the following...