“A bicycle leaning against a white wall.” by Ruslan Bardash on Unsplash

The object keyword does 2 things at the same time

There are 3 situations when it is used

Object declarations (singletons)

Singletons in OOP are used when you need just one instance of a class. An example would be the state of a video game.

In object declarations are allowed properties, methods, initilizer blocks etc. but not constructors since object declarations are created at the point of definition and never from calls to them. There can not be any:var state = GameState()

Companion objects

Companion objects are used when you need to call the internals of a class from an function that does not need an instance.


It is “kind of” a substitution of static in Java. Top level functions can be used in the same way with the difference that they can not access the private members of the class.

Companion objects as factory methods

The factory method pattern provide extra benefits to the standard object creation via constructors.

Only one companion object is allowed per class but we can name it for clarity if we want.

Companion objects and extension functions

If you want to separate certain logic from your class you can do it adding extension functions to a companion object.

Companion objects implementing an interface

Because companion objects can implement interfaces we could use the name of the containing class directly as an instance of an object implementing the interface.

Object expressions

Object expression are use to declare anonymous objects. Anonymous objects substitute Java’s anonymous inner classes.

The parameter of setAnimatorListener is an AnimatorListener object, using an object expression we declare and instantiate it directly on the function parameter. Note that there is no need to assign a name to it.

object expressions:

Android-Kotlin Developer

Android-Kotlin Developer