Object Expressions & Declarations
An object declaration creates a single shared instance (a singleton), while an object expression creates an anonymous one-off object on the spot, much like an anonymous class.
Learn Object Expressions & Declarations in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll learn the singleton object declaration, the anonymous object : Type expression, how a companion object differs, and implementing an interface inline.
What You'll Learn in This Lesson
1️⃣ Object Declarations: One Shared Instance
An object declaration — object Name {' '} — defines a singleton : exactly one instance, created the first time you use it. There's no constructor and no new ; you access members straight through the name. It's perfect for global state, services, and configuration.
Because there's only ever one Counter , two references to it are identical ( === is true) and they share the same count .
2️⃣ Object Expressions: Anonymous, One-Off Objects
An object expression — object : Type {' '} used as a value — creates a fresh anonymous object right where you need it, like Java's anonymous classes. It's the idiomatic way to pass a quick one-off implementation of an interface into a function.
Unlike a declaration, an expression runs every time the line executes and produces a new object each time — there's no single shared instance.
3️⃣ Companion Objects: A Singleton Inside a Class
A companion object is an object declared inside a class with the companion keyword. Its members are accessed through the class name — the closest Kotlin gets to Java's static . Use it for factory functions and constants that belong to the class itself.
Choose a companion object when the singleton belongs to one class (factories, constants); choose a standalone object declaration when it's an independent global like a logger.
Your turn. Fill in the ___ , then run and compare.
Combine a singleton object declaration with an inline object expression implementing an interface.
📋 Quick Reference — Objects
Practice quiz
What does an object declaration like object Counter { } create?
- A new instance every time it is referenced
- A singleton: exactly one shared instance
- An abstract class
- A companion object
Answer: A singleton: exactly one shared instance. An object declaration is a singleton with one instance created lazily on first access.
When is a Kotlin object declaration initialized?
- At program startup, always
- Lazily, the first time it is accessed
- Each time it is referenced
- Only when you call new on it
Answer: Lazily, the first time it is accessed. Object declarations are initialized lazily and safely on first access.
Given val a = Counter and val b = Counter, what does a === b print?
- false
- It does not compile
- true
- It throws at runtime
Answer: true. There is only one Counter instance, so both references are identical.
What does an object expression such as object : Listener { } produce?
- A singleton shared across the program
- A companion object
- A fresh anonymous object each time the line runs
- A constant
Answer: A fresh anonymous object each time the line runs. An object expression creates a new anonymous object every time it is evaluated.
How do you access a member of a companion object?
- Through an instance, like user.create()
- Through the class name, like User.create()
- With the new keyword
- Only inside the constructor
Answer: Through the class name, like User.create(). Companion members are accessed through the class name, similar to Java statics.
What is a companion object best used for?
- Creating many instances quickly
- Factory functions and constants tied to a class
- Replacing interfaces
- Holding thread-local data only
Answer: Factory functions and constants tied to a class. Companion objects hold factory methods and constants that belong to the class.
Why does object X(val a: Int) fail to compile?
- Objects cannot have properties
- Object declarations take no constructor parameters
- val is not allowed in objects
- It needs the new keyword
Answer: Object declarations take no constructor parameters. Object declarations have no constructor; initialize state in the body instead.
Which is the idiomatic Kotlin replacement for a Java anonymous class?
- A companion object
- A data class
- An object expression
- A sealed class
Answer: An object expression. Object expressions are Kotlin's clean replacement for Java anonymous classes.
Can an object declaration implement an interface?
- No, only classes can
- Yes, it can implement interfaces and act as a singleton service
- Only if it is a companion object
- Only inside another object
Answer: Yes, it can implement interfaces and act as a singleton service. Both object declarations and expressions can implement interfaces.
How do you call increment() on object Counter { fun increment() }?
- Counter().increment()
- new Counter().increment()
- Counter.increment()
- Counter::increment()
Answer: Counter.increment(). Access object members directly through the name; there is no constructor call.