We sometimes need singletons in our programming. A singleton is a single static global instance of some object, where that instance is the only possible instance of its class.
A top-level val
property is close to a singleton, in that it is a static global
instance of some object. However, other code could create an instance of the class
via the class' constructor. So, here, QUASI_SINGLETON
is not truly a singleton.
You can learn more about this in:
Tags:
xxxxxxxxxx
1
class Transmogrifier {
2
fun transmogrify() {
3
println("Presto, change-o!")
4
}
5
}
6
7
val QUASI_SINGLETON = Transmogrifier()
8
9
fun main() {
10
QUASI_SINGLETON.transmogrify()
11
}