🚀 Enrollments Open for Jetpack Compose Cohort 2 — 7 Days of Live Learning to Build Modern Android UIs 💚Join Now
KotlinIntermediate4 min
Singleton Pattern in Kotlin with object keyword?

Answer

In Kotlin, the `object` keyword is the idiomatic way to create a Singleton.

Features

  • Thread Safety: It is thread-safe by default. The instance is created lazily when the class is first accessed.
  • Simplicity: No need for static blocks, double-checked locking, or private constructors.

```kotlin object Database { val name = "MainDB" fun connect() { ... } }

// Usage Database.connect() ```

Under the hood, it compiles to a Java class with a static `INSTANCE` field.

Want to master these concepts?

Join our live cohorts and build production-ready Android apps.

Accelerate Your Growth

Don't just learn concepts in isolation. Build production-ready Android apps with expert guidance.

Live Interactive Sessions
Code Reviews & Feedback
Real-world Projects
Career Guidance

Limited seats available for next cohort