How to create a Singleton class in Kotlin? #4
Answered
by
furysniper2015
mohamedebrahim96
asked this question in
Q&A
-
How to create a Singleton class in Kotlin? |
Beta Was this translation helpful? Give feedback.
Answered by
furysniper2015
Jun 11, 2022
Replies: 1 comment
-
class UtilProject { class AnotherClass { fun main(args: Array) { |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
mohamedebrahim96
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
class UtilProject {
....
companion object {
val instance = UtilProject()
}
}
class AnotherClass {
...
companion object {
val instance = AnotherClass()
const val abc = "ABC"
}
}
fun main(args: Array) {
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
}