0Pricing
Kotlin Academy · Lesson

object Declaration: Kotlin Singletons

Create singletons with object and understand their lifecycle.

object Declaration: Kotlin Singletons is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “object Declaration: Kotlin Singletons” lesson free?

Yes — the full text of “object Declaration: Kotlin Singletons” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. object Declaration: Kotlin Singletons
  2. companion object: Static-like Members in Kotlin
  3. Anonymous Objects and Object Expressions
  4. Factory Methods with companion object
← Back to Kotlin Academy