Objects, Companions & Enums
Singletons with object, companion objects for factory functions and constants, and enum classes.
Kotlin has no static keyword. A singleton is declared with object, class-level members live in a companion object, and enum classes can carry properties and methods.
Examples
object Registry is created once, on first use, and accessed by name.
The private constructor forces callers through Packet.of, a factory in the companion object that validates the payload size.
const val MAX_PAYLOAD is a constant accessible as Packet.MAX_PAYLOAD.
Level.entries lists all enum values (Kotlin 1.9+); each has name, ordinal and its own properties.
Level.values() // allocates a new array each call entries (Kotlin 1.9+) returns an immutable, cached list; values() creates a fresh array every time.
What is a companion object?
An object declared inside a class whose members can be called through the class name (like Java static members), often used for factories and constants.