November 2018
Intermediate to advanced
388 pages
9h 5m
English
So far, we have explored how to implement a singleton pattern by making a constructor of the class private and providing a global way of accessing the instance that is initialized within the class in Java.
In Kotlin, it is much simpler to write a singleton class:
object Singleton {}
The object keyword is used for a singleton class. Note that we used object to define constants and static functions in our examples in the previous chapters. We indeed created a singleton class in these examples. Unlike the class, the object does not have any constructors.
In addition, we don't need to create the instance to access any of its functions. Let's add the following function to the singleton class:
Read now
Unlock full access