I am new to Kotlin. Still learning basic syntax. I've heard about companion objects similar to static in Java. But don't know how to create synchronized singleton in Kotlin.
Asked
Active
Viewed 7,865 times
4
-
2What do you mean by synchronized singleton? Something like this in Java: class Foo { private static Foo instance; public static synchronized Foo getInstance(){ if (instance == null){instance=new Foo();} return instance; } } – fgasparini Jun 30 '19 at 13:34
-
@fgasparini.. Yes. – New Android Developer Jun 30 '19 at 14:02
4 Answers
12
Just use
object Singleton {
// any members you need
}
It's already synchronized properly:
Note that this doesn't guarantee calls on it thread-safe, but that's just as in Java.

Alexey Romanov
- 167,066
- 35
- 309
- 487
-
1The OP seems to want his singleton to be lazy. This one is not. – G. Blake Meike Jul 01 '19 at 15:01
-
1@G.BlakeMeike Actually it is; it will be initialized when its class is loaded, and this only happens when it's first accessed. – Alexey Romanov Jul 01 '19 at 16:29
-
@G.BlakeMeike This applies in Java as well: https://stackoverflow.com/questions/15019306/regarding-static-holder-singleton-pattern – Alexey Romanov Jul 01 '19 at 16:33
-
Exactly. When the class is loaded, the first time the class is named. That is not lazy in the sense that the OP seemed to want. – G. Blake Meike Jul 01 '19 at 20:54
-
@G.BlakeMeike Do you mean that Java code can have other `static` members which can be accessed without calling `getInstance()`? In Kotlin this corresponds simply to putting them on another `object` (including a nested one). – Alexey Romanov Jul 02 '19 at 07:19
-
Does't work when the singleton needs an argument, like in Real Life. – Jeffrey Blattman Feb 13 '20 at 21:17
2
There are also other ways, but those two are the most simple ones to generate a singleton class
Method 1- Kotlin Way
object SingletonObj {
init {
// do your initialization stuff
}
}
Method 2- Double Null Check Way in Kotlin
class SingletonObj {
private constructor(context: Context)
companion object {
@Volatile private var mInstance: SingletonObj? = null
public fun get(context: Context): SingletonObj =
mInstance ?: synchronized(this) {
val newInstance = mInstance ?: SingletonObj(context).also { mInstance = it }
newInstance
}
}
}

hanilozmen
- 460
- 4
- 8
-
2now `synchronized ` deprecated, is there any replacement for it? – Roman Vasilyev Apr 04 '20 at 07:40
-
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/synchronized.html @RomanVasilyev – Mostafa Amer Apr 03 '21 at 19:01
2
Thread-safe and lazy:
class Singleton private constructor() {
companion object {
val instance: Singleton by lazy { Singleton() }
}
}
Double null check already implemented inside by lazy
.

Prilaga
- 818
- 10
- 18
1
I think, little bit more research, and i found it . Here is how to do it . Please correct me if it can be done in better way.
companion object {
@Volatile private var INSTANCE: Singleton ? = null
fun getInstance(): Singleton {
if(INSTANCE == null){
synchronized(this) {
INSTANCE = Singleton()
}
}
return INSTANCE!!
}
}

New Android Developer
- 81
- 1
- 9
-
@fgasparini , Request you to suggest if there is any better way . – New Android Developer Jun 30 '19 at 15:02
-
3That absolutely will not work. You need a second null check inside the synchronized block. ... and you might want to look at https://portabledroid.wordpress.com/2018/08/09/fast-locking-in-android-with-kotlin/ – G. Blake Meike Jun 30 '19 at 15:19
-
@G.BlakeMeike... can you please edit or post the corrected code – New Android Developer Jun 30 '19 at 16:17
-
Double checked locking has been proven to not work years ago. Kotlin runs on the JVM. Doesn't work for Java, either. Singleton is a discredited pattern. – duffymo Aug 11 '21 at 23:12