object Declaration: Kotlin Singletons
Create singletons with object and understand their lifecycle.
object Declaration: Kotlin Singletons is a free Kotlin Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
object as a Singleton
An object declaration creates a single instance — a thread-safe, lazily-initialized singleton — with one line of code.
Declaring a Singleton
Use object Name { ... } to declare a singleton with state and methods.
object Logger {
fun log(msg: String) = println("[LOG] $msg")
}
fun main() {
Logger.log("App started")
Logger.log("User signed in")
}Singleton State
Singletons can hold mutable state — accessible from anywhere, shared globally.
object Counter {
var count = 0
fun increment() { count++ }
}
fun main() {
Counter.increment()
Counter.increment()
Counter.increment()
println(Counter.count) // 3
}Lazy Initialization
The single instance is created the first time it is accessed — never sooner. The JVM guarantees this is thread-safe.
object Heavy {
init { println("Initializing Heavy...") }
fun work() = "done"
}
fun main() {
println("Before access")
println(Heavy.work()) // triggers init now
println(Heavy.work()) // no init again
}Implementing Interfaces
Singletons can implement interfaces, useful for callbacks and strategies.
interface Greeter { fun greet(name: String): String }
object EnglishGreeter : Greeter {
override fun greet(name: String) = "Hello, $name!"
}
fun main() {
println(EnglishGreeter.greet("Ada"))
}Extending a Class
Objects can inherit from open classes too.
open class Animal(val sound: String) {
fun makeSound() = "The animal says $sound"
}
object Cat : Animal("Meow")
fun main() {
println(Cat.makeSound())
}Practical: Configuration
Singletons are ideal for app-wide configuration that doesn't change after startup.
object AppConfig {
val apiUrl = "https://api.example.com"
val timeout = 5000
val version = "1.0.0"
}
fun main() {
println("v${AppConfig.version} -> ${AppConfig.apiUrl}")
}Practical: Utility Functions
Group related stateless helpers in an object as namespaced static-like methods.
object StringUtils {
fun reverse(s: String) = s.reversed()
fun capitalize(s: String) = s.replaceFirstChar { it.uppercase() }
}
fun main() {
println(StringUtils.reverse("Kotlin"))
println(StringUtils.capitalize("hello"))
}Equality and Identity
There is only one instance, so === always returns true for the same object reference.
object MySingleton
fun main() {
val a = MySingleton
val b = MySingleton
println(a === b) // true
}object vs class
A regular class needs instantiation. An object is the instance — there is no constructor invocation.
class Service { fun ping() = "pong (class)" }
object SingletonService { fun ping() = "pong (object)" }
fun main() {
val s = Service() // instance per call
println(s.ping())
println(SingletonService.ping()) // no instantiation
}Thread Safety
The singleton instance creation is thread-safe by JVM class-loading guarantees. Any mutable state you add inside is your responsibility.
object SharedCounter {
@Volatile var count = 0
@Synchronized fun inc() { count++ }
}
fun main() {
repeat(1000) { SharedCounter.inc() }
println(SharedCounter.count) // 1000
}Quick Check
How many instances exist of a Kotlin object declaration?
Recap
object Name { ... } declares a thread-safe lazy singleton with state, methods, interfaces, and inheritance. Use objects for configuration, utility groups, and stateless services.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “object Declaration: Kotlin Singletons” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “object Declaration: Kotlin Singletons”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
What will I learn in “object Declaration: Kotlin Singletons”?
Create singletons with object and understand their lifecycle. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “object Declaration: Kotlin Singletons” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- object Declaration: Kotlin Singletons
- companion object: Static-like Members in Kotlin
- Anonymous Objects and Object Expressions
- Factory Methods with companion object