Kotlin Object. Singleton, Anonymous, Companion Object
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:
- 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.
- Implementing an interface: An object can implement an interface, which is useful when you want to create a type that has a specific behavior.
- 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 object
s and how to use them, you can write more efficient and organized Kotlin code.
Tips for understanding:
- An object is a singleton instance of a class.
- It is created at the time of initialization and can be used throughout the program.
- An object can be used to create a singleton instance of a class, implement an interface, and simplify code.
- An object 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.
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 !