Skip to main content
Browse topics

Singleton Pattern in Kotlin

2 min read Updated June 30, 2026
Share

Introduction

Singleton Pattern is a fundamental concept every Kotlin developer should understand. Object declarations and companion objects provide singletons, factory methods, and static-like members without the ceremony of Java static blocks.

Singleton ensures only one instance of a class exists. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • Singleton ensures only one instance of a class exists.
  • Kotlin object declaration provides singleton by default.
  • It is thread-safe and concise.

Syntax

kotlin
object Config { val appName = "Demo" }

Singleton Pattern in Kotlin Example Program in Kotlin

kotlin
object AppConfig {
    const val APP_NAME = "Kotlin Learning"
}

fun main(args: Array<String>) {
    println(AppConfig.APP_NAME)
}

Sample Output

plaintext
Kotlin Learning

When to use

Use object for true singletons; use companion object for factory methods and constants tied to a class.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. const val APP_NAME = "Kotlin Learning" assigns or updates a value used later in the program.

  3. The println(AppConfig.APP_NAME) statement writes a line to the console — this produces part of the sample output below.

  4. Singleton ensures only one instance of a class exists.

  5. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

Common Mistakes

Key Points

  • Singleton ensures only one instance of a class exists.
  • Kotlin object declaration provides singleton by default.
  • It is thread-safe and concise.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is Singleton Pattern in Kotlin?
Singleton ensures only one instance of a class exists.
When should I use Singleton Pattern?
Use object for true singletons; use companion object for factory methods and constants tied to a class.
How is Singleton Pattern different from Java?
It is thread-safe and concise.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related tutorials

Search tutorials