I want to know how to create a singleton class in Kotlin, so that my Util class instantiates it only once per app execution. However, when I converted my Java class to kotlin, the code below was generated.
Is this correct?
companion object { private var utilProject: UtilProject? = null val instance: UtilProject get() { if (utilProject == null) utilProject = UtilProject() return utilProject!! } }
I could find a related question, but it is with parameters, and I am not getting it convert without params.
Using Singletons in Kotlin. By using the keyword object in your app, you're defining a singleton. A singleton is a design pattern in which a given class has only one single instance inside the entire app. A singleton's two most common use cases are: To share data between two otherwise unrelated areas of your project.
In Kotlin, the singleton pattern is used as a replacement for static members and fields that don't exist in that programming language. A singleton is created by simply declaring an object . Contrary to a class , an object can't have any constructor, but init blocks are allowed if some initialization code is needed.
The Singleton's purpose is to control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields.
There is a special keyword object
for singletons in Kotlin. You can just type something as simple as this to get working singleton class:
object MySingleton
or when you want some member functions:
object MySingleton { fun someFunction(...) {...} }
And then use it:
MySingleton.someFunction(...)
there is a reference: https://kotlinlang.org/docs/reference/object-declarations.html#object-declarations
EDIT:
In your case, you just need to replace in your definition of class UtilProject
to this:
object UtilProject { // here you put all member functions, values and variables // that you need in your singleton Util class, for example: val maxValue: Int = 100 fun compareInts(a: Int, b: Int): Int {...} }
And then you can simply use your singleton in other places:
UtilProject.compareInts(1, 2) //or var value = UtilProject.maxValue
Just
companion object { val instance = UtilProject() }
will do the job because the companion object itself is a language-level singleton.
(The instance
will be created when the companion object is first called.)
-- Updated --
If you need to control when the singleton object is initialized, you can create one object for each class.
class UtilProject { .... companion object { val instance = UtilProject() } } class AnotherClass { ... companion object { val instance = AnotherClass() const val abc = "ABC" } } fun main(args: Array<String>) { val a = UtilProject.instance // UtilProject.instance will be initialized here. val b = AnotherClass.abc // AnotherClass.instance will be initialized here because AnotherClass's companion object is instantiated. val c = AnotherClass.instance }
Here, AnotherClass.instance
is initialized before AnotherClass.instance
is actually called. It is initialized when AnotherClass
's companion object is called. To prevent it from being initialized before when it is needed, you can use like this:
class UtilProject { .... companion object { fun f() = ... } } class AnotherClass { ... companion object { const val abc = "ABC" } } object UtilProjectSingleton { val instance = UtilProject() } object AnotherClassSingleton { val instance = AnotherClass() } fun main(args: Array<String>) { UtilProject.f() println(AnotherClass.abc) val a = UtilProjectSingleton.instance // UtilProjectSingleton.instance will be initialized here. val b = AnotherClassSingleton.instance // AnotherClassSingleton.instance will be initialized here. val c = UtilProjectSingleton.instance // c is a. }
If you don't care when each singleton is initialized, you can also use it like this:
class UtilProject { .... companion object { fun f() = ... } } class AnotherClass { ... companion object { const val abc = "ABC" } } object Singletons { val utilProject = UtilProject() val anotherClass = AnotherClass() } fun main(args: Array<String>) { val a = Singletons.utilProject val b = Singletons.anotherClass }
In summary,
an object
or a companion object
is one singleton object in Kotlin.
You can assign variables in an object or objects, and then use the variables just like they were singletons.
object
or companion object
is instantiated when it is first used. val
s and var
s in an object
are initialized when the object
is first instantiated (i.e., when the object
is first used).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With