|
Many programming languages support static members. Kotlin does not support static members. Instead, you should use top-level functions or object declarations.
|
|
|
For functions that do not need state, a top-level function can be used.
|
|
|
For functions that do need state, we can use an object declaration, which essentially creates a singleton object. An object declaration can use interfaces or inheritance, just like a normal class.
|
fun getUniqueNumber(): Int {
private var internalCounter = 0
|
|
Functions in an object declaration can not access private or protected members of their arguments. To do this, a companion object can be used.
|
fun getNumber(c: Counter): Int {
return c.internalCounter++
println(Utils.getUniqueNumber())
println(Utils.getUniqueNumber())
println(Utils.getUniqueNumber())
println(Counter.getNumber(c))
println(Counter.getNumber(c))
println(Counter.getNumber(c))
|