Kotlin Object. Singleton, Anonymous, Companion Object

Manuchekhr Tursunov
3 min readApr 7, 2023

--

In Kotlin, an object is defined using the “object” keyword. It can contain properties, methods, and can also implement interfaces. The main difference between an object and a class is that an object can be instantiated only once, whereas a class can be instantiated multiple times.

An object can be used in various scenarios such as:

  1. Creating a Singleton instance: If you want to create a class that has only one instance across the application, then creating an object of the class is a good option.
  2. Implementing an interface: An object can implement an interface, which is useful when you want to create a type that has a specific behavior.
  3. Simplifying code: Using an object can simplify code by reducing the need to create multiple instances of the same class.

In Kotlin, an object is a special kind of class that is used to declare a singleton, an anonymous object, or a companion object. Here are some explanations and code examples for each of these use cases:

  • Singleton Object

A singleton object is a single instance of a class that can be used throughout your application. It is created lazily when it is first accessed, and subsequent accesses return the same instance. You can define a singleton object in Kotlin using the object keyword.

object MySingleton {
fun doSomething() {
// implementation
}
}

To use the singleton object, simply call its methods like this:

MySingleton.doSomething()
  • Anonymous Object

An anonymous object is a short-lived object that is used to implement an interface or extend a class without creating a named subclass. You can define an anonymous object in Kotlin using the object keyword

val myObj = object : MyInterface {
override fun doSomething() {
// implementation
}
}

To use the anonymous object, simply call its methods like this:

myObj.doSomething()
fun doSomething() {
val obj = object {
var x = 0
var y = 0

fun incrementX() {
x++
}

fun incrementY() {
y++
}
}

obj.incrementX()
obj.incrementY()
println("x = ${obj.x}, y = ${obj.y}")
}

// Usage:
doSomething() // Output: x = 1, y = 1
  • Companion Object

A companion object is an object that is tied to the class in which it is defined. It can be used to store static methods and properties. You can define a companion object in Kotlin using the companion object keyword

class MyClass {
companion object {
fun doSomething() {
// implementation
}
}
}

To use the companion object, simply call its methods like this:

MyClass.doSomething()

In summary, an object in Kotlin can be used to define a singleton, an anonymous object, or a companion object. Singleton objects provide a global instance of a class, anonymous objects are used to implement interfaces or extend classes without creating a named subclass, and companion objects are tied to the class in which they are defined and can be used to store static methods and properties. By understanding the different types of objects and how to use them, you can write more efficient and organized Kotlin code.

Tips for understanding:

  1. An object is a singleton instance of a class.
  2. It is created at the time of initialization and can be used throughout the program.
  3. An object can be used to create a singleton instance of a class, implement an interface, and simplify code.
  4. An object can contain properties, methods, and can also implement interfaces.
  5. The main difference between an object and a class is that an object can be instantiated only once, whereas a class can be instantiated multiple times.

Follow me on Twitter
For more articles, please Subscribe
I would really appreciate If you Support Me -> Buy me a coffee on Ko-fi
Thanks !

--

--