Naresh Beniwal
How can you create a singleton in Kotlin?
By Naresh Beniwal in Kotlin on Aug 09 2024
  • Adarsh Nigam
    Aug, 2024 10

    **Question:** How do you create a singleton in Kotlin?**Answer:** In Kotlin, you create a singleton using the `object` keyword. This defines a class and simultaneously creates a single instance of it. For example:object Singleton {val property = "Singleton Property"fun show() {println(property)} }The `Singleton` object is instantiated only once, and you can access its properties and methods directly without needing to create an instance. Kotlin's singletons are also thread-safe by default.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS