What is the difference between an object and a companion object in a class in kotlin?
Example:
class MyClass {
object Holder {
//somethi
Objects can implement interfaces. Inside a class, defining a simple object that doesn't implement any interfaces has no benefit in most cases. However, defining multiple objects that implement various interfaces (e.g. Comparator
) can be very useful.
In terms of lifecycle, there is no difference between a companion object and a named object declared in a class.