0
0
Kotlinprogramming~10 mins

Why object declarations create singletons in Kotlin - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why object declarations create singletons
Start program
Declare object
Object instance created once
Access object multiple times
Same instance used every time
Program ends
The program declares an object which creates a single instance. Every access uses this same instance, ensuring only one exists.
Execution Sample
Kotlin
object Logger {
    fun log(msg: String) = println("Log: $msg")
}

fun main() {
    Logger.log("Start")
    Logger.log("End")
}
This code declares a singleton object Logger and calls its log function twice, using the same instance.
Execution Table
StepActionEvaluationResult
1Program startsNo object instance yetNo instance created
2Object Logger declaredObject declaration does not immediately create instanceInstance created at first access
3Call Logger.log("Start")Instance created at first access, then usedPrints: Log: Start
4Call Logger.log("End")Use same Logger instancePrints: Log: End
5Program endsNo new instances createdOnly one Logger instance used
💡 Program ends after using the single Logger instance multiple times
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Logger instancenullnullcreatedusedused
Key Moments - 3 Insights
Why is the Logger instance created only once?
Because object declarations in Kotlin create a singleton instance at first access, as shown in execution_table step 3.
Does calling Logger.log create a new Logger instance each time?
No, the same Logger instance is reused every time, as shown in steps 3 and 4 of the execution_table.
What if we declare Logger as a class instead of object?
Then each time we create Logger(), a new instance is made. But with object, Kotlin ensures only one instance exists.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the Logger instance created?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Check the 'Result' column in execution_table row for Step 3
According to variable_tracker, what is the state of Logger instance after Step 4?
Anull
Bcreated
Cused
Ddestroyed
💡 Hint
Look at the 'Logger instance' row and the 'After Step 4' column in variable_tracker
If Logger was declared as a class, how would the execution_table change?
AInstance created at every call to log()
BInstance created once at Step 2
CNo instance created
DInstance created only at program end
💡 Hint
Recall that class declarations create new instances on each constructor call, unlike object declarations
Concept Snapshot
Kotlin object declarations create a singleton instance.
This instance is created once at first access.
All uses share the same instance.
Use object for single-instance utilities.
Unlike classes, no new instances are made on usage.
Full Transcript
In Kotlin, when you declare an object, the system creates exactly one instance of it. This instance is made the first time the object is accessed. Every time you use the object later, you get the same instance. For example, the Logger object is created once when the program starts. When Logger.log is called twice, both calls use the same Logger instance. This is different from classes, where each new object is a new instance. Objects are useful when you want a single shared instance, like for logging or configuration.