Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: Difference between object and companion object in a class

What is the difference between an object and a companion object in a class in kotlin?

Example:

class MyClass {      object Holder {         //something     }      companion object {         //something     } } 

I already read that companion object shall be used, if the containing parameters/methods are closely related to its class.

But why is there also the possibility of declaring a normal object in the class? Because it behaves exactly like the companion, but it must have a name.

Is there maybe a difference in its "static" (I'm from the java side) lifecycle?

like image 407
Poweranimal Avatar asked May 05 '17 22:05

Poweranimal


People also ask

What is companion object in Kotlin?

Kotlin Android. Unlike Java or C#, Kotlin doesn't have static members or member functions. Kotlin recommends to simply use package-level functions instead.

What is difference between singleton object and companion object?

A singleton object named the same as a class is called a companion object. Also a companion object must be defined inside the same source file as the class.

What is the benefit of companion object?

Advantages of Companion Objects in Scala Companion objects provide a clear separation between static and non-static methods in a class because everything that is located inside a companion object is not a part of the class's runtime objects but is available from a static context and vice versa.

What is a companion object?

A companion object is an object that's declared in the same file as a class , and has the same name as the class. A companion object and its class can access each other's private members. A companion object's apply method lets you create new instances of a class without using the new keyword.


Video Answer


2 Answers

There are two different types of object uses, expression and declaration.

Object Expression

An object expression can be used when a class needs slight modification, but it's not necessary to create an entirely new subclass for it. Anonymous inner classes are a good example of this.

button.setOnClickListener(object: View.OnClickListener() {     override fun onClick(view: View) {         // click event     } }) 

One thing to watch out for is that anonymous inner classes can access variables from the enclosing scope, and these variables do not have to be final. This means that a variable used inside an anonymous inner class that is not considered final can change value unexpectedly before it is accessed.

Object Declaration

An object declaration is similar to a variable declaration and therefore cannot be used on the right side of an assignment statement. Object declarations are very useful for implementing the Singleton pattern.

object MySingletonObject {     fun getInstance(): MySingletonObject {         // return single instance of object     } } 

And the getInstance method can then be invoked like this.

MySingletonObject.getInstance() 

Companion Object

A companion object is a specific type of object declaration that allows an object to act similar to static objects in other languages (such as Java). Adding companion to the object declaration allows for adding the "static" functionality to an object even though the actual static concept does not exist in Kotlin. Here's an example of a class with instance methods and companion methods.

class MyClass {   companion object MyCompanionObject {     fun actsAsStatic() {       // do stuff     }   }      fun instanceMethod() {     // do stuff   } } 

Invoking the instance method would look like this.

var myClass = MyClass() myClass.instanceMethod() 

Invoking the companion object method would look like this.

MyClass.actsAsStatic() 

See the Kotlin docs for more info.

like image 86
Mike Avatar answered Sep 23 '22 03:09

Mike


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.

like image 36
yole Avatar answered Sep 21 '22 03:09

yole