Objects & Companions
object — singletons
The object keyword declares a class and its single instance at once:
object AppConfig {
val version = "1.0"
fun describe() = "App v$version"
}
AppConfig.version // access directly — no constructor
Use it for stateless helpers, registries, or anything there should be exactly one of.
Companion objects
A companion object holds members tied to the class itself rather than to
instances — the closest thing to Java’s static:
class User private constructor(val name: String) {
companion object {
fun create(name: String): User = User(name.trim()) // factory
const val MAX_NAME_LENGTH = 50
}
}
val u = User.create(" Ada ") // call on the class
User.MAX_NAME_LENGTH // 50
A common pattern: make the constructor private and expose a factory method on
the companion that validates input.
Anonymous objects
Create a one-off object, often to implement an interface on the spot:
interface Listener { fun onEvent(name: String) }
val logger = object : Listener {
override fun onEvent(name: String) = println("event: $name")
}
Object expressions vs declarations
object Name { ... }— a named singleton (declaration).object : Type { ... }— an anonymous instance (expression), evaluated where it appears.
Exercises
-
Create an
object Counterwith a privatevar countandincrement()/current()functions. -
Give a
Colorclass acompanion objectwith a factoryfromHex(hex: String)and a constantBLACK.
Previous: Data, Enum & Sealed Classes · Next: Extension Functions