Object and Companion Object
object declares a singleton. A companion object holds members that belong to the class, replacing static.
Example
object Config { val name = "JTS" }
class Counter {
companion object { var total = 0; fun next() = ++total }
}
println(Config.name)
println(Counter.next())
println(Counter.next())Exercise
Singleton keyword.
Logger { }Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.