Beginner 6 min readKotlin 2.0

Kotlin object โ€” Singletons and Utilities

The object keyword declares a singleton โ€” one instance for the entire application, created on first access.

What You Will Learn

  • Declare top-level singleton objects
  • Use object for utility APIs
  • Create anonymous objects
  • Compare object vs class vs companion
  • Understand thread-safe initialization

Singleton with object

object Logger creates a single instance. No constructor call needed โ€” use Logger directly.

Utility Object

kotlin
object Logger {
    fun info(msg: String) = println("[INFO] $msg")
    fun error(msg: String) = println("[ERROR] $msg")
}

fun main() {
    Logger.info("App started")
    Logger.error("Something failed")
}
Output
[INFO] App started [ERROR] Something failed

Logger is initialized lazily on first use. All code shares the same instance.

Anonymous object

Create one-off objects without naming a new class file โ€” useful for listeners and adapters.

Anonymous Object

kotlin
interface ClickListener {
    fun onClick()
}

fun main() {
    val listener = object : ClickListener {
        override fun onClick() = println("Clicked!")
    }
    listener.onClick()
}
Output
Clicked!

object : Interface creates an anonymous implementation inline.

Practice Exercise

Exercisemultiple choice

What does a top-level object declaration create?

Quick Quiz

Quick Quiz

object vs class โ€” main difference?

Frequently Asked Questions

Related Tutorials

Last updated: 2026-05-19Kotlin 2.0

Written by KotlinGuide Editorial Team ยท Reviewed by KotlinGuide Technical Review