An object expression creates an anonymous object at the point where it is written. It can implement one or more interfaces, extend a class, or stand alone, making it Kotlin’s concise replacement for Java anonymous inner classes. section 4.4.4 section 4.6
Create an event listener inline
Button(object : MouseListener { override fun onEnter() { /* ... */ } override fun onClick() { /* ... */ } }) The object expression declares an unnamed class and immediately creates its instance. Omitting a name distinguishes it from an object declaration. Each execution creates a new instance, rather than reusing a singleton. section 4.4.4
Use an object expression when the implementation needs a compact, local object:
- Implement multiple interfaces or provide several overridden methods in one local definition. section 4.4.4 section 4.6
- Capture local variables from the surrounding function; unlike Java’s restriction, Kotlin also permits modifying them. section 4.4.4
- Prefer a lambda or SAM conversion for a single-method interface when an object instance is unnecessary. section 4.4.4
Capture mutable state
var clickCount = 0 Button(object : MouseListener { override fun onEnter() { /* ... */ } override fun onClick() { clickCount++ } }) The listener can update the surrounding function’s local variable, which is useful for small pieces of event-handling state.
An anonymous object has its own this reference, unlike a lambda. That makes it appropriate when a listener must refer to itself, such as unsubscribing from an event source while handling an event. section 5.2.2

