Singleton Pattern in Kotlin
On this page (12sections)
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
object Config { val appName = "Demo" }
Singleton Pattern in Kotlin Example Program in Kotlin
object AppConfig {
const val APP_NAME = "Kotlin Learning"
}
fun main(args: Array<String>) {
println(AppConfig.APP_NAME)
}
Sample Output
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
-
The program starts with a
mainfunction — the entry point that runs when you execute the file. -
const val APP_NAME = "Kotlin Learning"assigns or updates a value used later in the program. -
The
println(AppConfig.APP_NAME)statement writes a line to the console — this produces part of the sample output below. -
Singleton ensures only one instance of a class exists.
-
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
- Understand the core idea: singleton ensures only one instance of a class exists.
- Prefer readable names and small functions so examples map directly to real projects.
- Run and modify the example — change values and observe how the output changes.
Common Mistakes
- Skipping the example and only reading the definition — hands-on practice cements the concept.
- Copying syntax without understanding nullable vs non-nullable types or scope rules.
- Ignoring compiler warnings that often point to safer alternatives.
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?
When should I use Singleton Pattern?
How is Singleton Pattern different from Java?
How do I practice this topic?
Related Tutorials
Object Expressions in Kotlin
Learn Object Expressions in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorialEnum Classes in Kotlin
Learn Enum Classes in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorial