Singleton Class in Kotlin

Deepak Sikka
5 min readJun 22, 2021

In this blog, we will learn how to make a singleton class in Kotlin? So, let’s get started.

In Android App, for an object which is required to be created only once and use everywhere, we use the Singleton Pattern. Singleton Pattern is a software design pattern that restricts the instantiation of the class to only “one” instance. So, to implement the Singleton pattern in our project or software, we make a singleton class.

Singleton Class :

Singleton Class in Kotlin is also called as the Singleton Object in Kotlin.A singleton class is a class that is defined in such a way that only one instance of the class can be created and used everywhere.

1). It is used where we need only one instance of the class like NetworkService, DatabaseService, etc.

Properties of Singleton Class :

Following are the properties of a typical singleton class:

  1. Only one instance: The singleton class has only one instance and this is done by providing an instance of the class, within the class. Also, outer classes and subclasses should be prevented to create the instance.
  2. Globally accessible: The instance of the singleton class should be globally accessible so that each class can use it.

Rules for making a class Singleton :

The following rules are followed to make a Singleton class:

  1. A private constructor
  2. A static reference of its class
  3. One static method
  4. Globally accessible object reference
  5. Consistency across multiple threads

Following is the example of Singleton class in Java:

public class Singleton {

private static Singleton instance = null;

private Singleton() {

}

public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}

Let’s look at the Kotlin code for the same. Below is the Kotlin code for Singleton class:

fun main(){
val obj= Singleton
val obj1= Singleton
println(obj1.toString())
println(obj.toString())
}
// SingleTon
object Singleton{

}

In Kotlin, we need to use the object keyword. The object class can have functions, properties, and the init method.The constructor method is not allowed in an object so we can use the init method if some initialization is required and the object can be defined inside a class. We call the method and member variables present in the singleton class using the class name, as we did in the companion objects

fun main() {
println(Demo.name)
println("The answer of addition is ${Demo.add(3,2)}")
println("The answer of addition is ${Demo.add(10,15)}")
}

object Demo {
init {
println("Singleton class invoked.")
}

var name = "GFG Is Best"
fun add(num1: Int, num2: Int): Int {
return num1.minus(num2)
}
}

Sample Android Program showing Use of Singleton Object:

const val  BASE_URL = "https://newsapi.org/"
const val API_KEY = "ff30357667f94aca9793cc35b9e447c1"

interface NewsInterface{
@GET("v2/top-headlines?apiKey=$API_KEY")
fun getheadlines
(@Query("country")country:String,@Query("page")page:Int):Call<News>
}

object NewsService
{
val newsInstance:NewsInterface
init {
val retrofit=Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
newsInstance=retrofit.create(NewsInterface::class.java)
}
}

Object extending a class :

You can use an object in Kotlin to extend some class or implement some interface just like a normal class. Let’s have an example of the same:

object singleWithExtendClass : CommonClass() {
init {
println("Single Class is invoked.")
}

var variableName = "I am Deepak Sikka"
override fun printVarName() {
println(variableName)
}

}

open class CommonClass {
open fun printVarName() {
print("User Name")
}

init {
println("I am in init of A")
}
}

fun main() {
val ob = CommonClass()
println(ob.toString())

singleWithExtendClass.printVarName()
println(singleWithExtendClass.toString())
}

So, you can use object class just like a normal class in most of the cases.

Singleton class with Argument in Kotlin

In the earlier part of the blog, we learned that we can’t have constructors in a singleton class. To initialize something, we can do so by using init in the singleton class. But what if you need to pass some argument for initialization just like in parameterized constructors? Since we can’t use constructors here. So, we need to find some other way of doing the same.

If we particularly talk about Android, we know that in Android we generally need to pass a context instance to init block of a singleton. This can be done using Early initialization and Lazy initialization. In early initialization, all the components are initialized in the Application.onCreate() using the init() functions. But this results in slowing down the application startup by blocking the main thread. So, it is generally advised to use the lazy initialization way. In lazy initialization, we use the context as an argument to a function returning the instance of the singleton. We can achieve this by using a SingletonHolder class. Also, to make it thread-safe, we need to have a way of synchronization and double-checked locking.

open class SingletonHolder<out T: Any, in A>(creator: (A) -> T) {
private var creator: ((A) -> T)? = creator
@Volatile private var instance: T? = null

fun getInstance(arg: A): T {
val checkInstance = instance
if (checkInstance != null) {
return checkInstance
}

return synchronized(this) {
val checkInstanceAgain = instance
if (checkInstanceAgain != null) {
checkInstanceAgain
} else {
val created = creator!!(arg)
instance = created
creator = null
created
}
}
}
}

The above code is the most efficient code for double-checked locking system and the code is somehow similar to the lazy() function in Kotlin and that’s why it is called lazy initialization. So, whenever you want a singleton class with arguments then you can use the SingletonHolder class.

Here, in the above code, in place of the creator function which is passed as an argument to the SingletonHolder, a custom lambda can also be declared inline or we can pass a reference to the private constructor of the singleton class. So, the code will be:

class YourManager private constructor(context: Context) {
init {
// do something with context
}
companion object : SingletonHolder<YourManager, Context (::YourManager)
}

Now, the singleton can be easily invoked and initialized by writing the below code and this is lazy as well as thread-safe :)

YourManager.getInstance(context).doSomething()

That’s it for the Singleton blog. Hope you like this blog. You can also refer to the Kotlin website.

Keep Learning :)

--

--

Deepak Sikka

Senior Android Developer. Working on technology Like Java,Kotlin, JavaScript.Exploring Block Chain technology in simple words.