Introduction
Kotlin, with its concise syntax and powerful features, has gained immense popularity among developers worldwide. One such feature that Kotlin offers is the Singleton class. Singleton is a design pattern that ensures a class has only one instance and provides a global point of access to it. In this blog, we will discuss singleton class in Kotlin in detail.

Singleton Class in Kotlin
A singleton class in Kotlin is designed so only a single instance can be created and used everywhere. Instances of the class that only require one are NetworkService, DatabaseService, etc. It is typically done because creating these objects repeatedly uses system resources. Therefore, creating an object only once and using it repeatedly is better.
Let us take an example:
Imagine that we are creating a kingdom and that only one path can be used to enter or leave the kingdom. That is how Kotlin's Singleton Class works.

Let us take an example of a normal class, and then it will be easy for you to understand the difference between a normal class and a singleton class.
Example of a Normal Class
class Student(){
fun run(){
println("Running")
}
}
fun main(){
val t1 = Student()
val t2 = Student()
println(t1)
println(t2)
}
Output

Thus, "t1" and "t2" each have two distinct memory locations. Let us now take an example of a Singleton class.
Rules for making the Singleton class in Kotlin
To create a Singleton class, the following guidelines must be followed:
- A private constructor
- A static reference of its class
- One static method
- Globally accessible object reference
- Consistency across multiple threads
To create a singleton class, we use the “object” keyword. Yes, it is that simple. There is a long process to make a class singleton in Java, but Kotlin is a lifesaver language.
In Kotlin, we must use the “object” keyword to use the Singleton class, as already discussed above. The object class can have properties, functions, and the init method.
Key Point 👀: The constructor method is prohibited in an object, so if some initialization is needed, we can use the init method and the object can be defined inside a class. The object gets instantiated when it is used for the first time.
Example of a Singleton Class
object Singleton{
init{
println("Singleton method invoked successfully!")
}
fun run(){
println("hello Ninja")
}
}
fun main(){
val t1 = Singleton
val t2 = Singleton
println(t1)
println(t2)
}
Output

Although it appears that we are creating two objects on the left side of the code, in reality, we are only giving the exact memory location two different names. This is because the memory addresses for both objects are the same.
When to apply the Singleton pattern?
Below are the mentioned applications of the Singleton pattern:

Use the Singleton pattern when a class should have a single instance available to all clients, for example, a single database object shared by different program parts.
-
Except for the unique creation method, the Singleton pattern prevents the creation of objects using any other methods. If an object has already been created, this method either returns it or creates a new one if necessary.
When you require tighter control over global variables, use the Singleton pattern.
- The Singleton pattern ensures that there must be one instance of a class, in contrast to global variables. The Singleton class itself can only replace the cached instance.





