Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use "companion object" as a kind of replacement for Java static fields in Kotlin?

What is the intended meaning of "companion object"? So far I have been using it just to replace Java's static when I need it.

I am confused with:

  • Why is it called "companion"?
  • Does it mean that to create multiple static properties, I have to group it together inside companion object block?
  • To instantly create a singleton instance that is scoped to a class, I often write

:

companion object {     val singleton by lazy { ... } } 

which seems like an unidiomatic way of doing it. What's the better way?

like image 365
Randy Sugianto 'Yuku' Avatar asked Jul 14 '16 18:07

Randy Sugianto 'Yuku'


People also ask

Why do we use companion object in Kotlin?

companion object is how you define static variables/methods in Kotlin. You are not supposed to create a new instance of Retrofit / ApiService each time you execute a request, however.

Why do we use companion object?

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. A companion object's unapply method lets you de-construct an instance of a class into its individual components.

Is companion object static Kotlin?

Answer: Companion object is not a Singleton object or Pattern in Kotlin – It's primarily used to define class level variables and methods called static variables. This is common across all instances of the class.

How do you use companion objects in Java?

Declaring a companion objectIf we declare the object inside a class, we have an option to mark it as a companion object. In terms of Java, the members of the companion object can be accessed as static members of the class. Marking an object as a companion allows us to omit the object's name while calling its members.

How to use companion object in Kotlin?

Like a singleton, but to access the singleton object you have to initialize the "outer" class first. We can say that companion is same as "Static Block" like Java, But in case of Kotlin there is no Static Block concept, than companion comes into the frame. class Example { companion object { fun display () { //place your code } } }

Why doesn't Kotlin use static members like in Java?

First, Kotlin doesn't use the Java concept of static members because Kotlin has its own concept of object s for describing properties and functions connected with singleton state, and Java static part of a class can be elegantly expressed in terms of singleton: it's a singleton object that can be called by the class' name.

What is @jvmstatic annotation in Kotlin?

These are used to provide static behavior to parameters or methods defined as part of the companion object. Also, in order to have the Kotlin companion object used across the other Java code in the application, you can use @JvmStatic annotation to have interoperability with Java.

What is a companion object in Java?

A few important points to remember while defining companion objects are: Companion objects cannot be declared outside a class. Companion objects can be used similar to static classes in other programming languages like Java and C#. These objects are common in all instances of the class.


1 Answers

  • What is the intended meaning of "companion object"? Why is it called "companion"?

    First, Kotlin doesn't use the Java concept of static members because Kotlin has its own concept of objects for describing properties and functions connected with singleton state, and Java static part of a class can be elegantly expressed in terms of singleton: it's a singleton object that can be called by the class' name. Hence the naming: it's an object that comes with a class.

    Its name used to be class object and default object, but then it got renamed to companion object which is more clear and is also consistent with Scala companion objects.

    Apart from naming, it is more powerful than Java static members: it can extend classes and interfaces, and you can reference and pass it around just like other objects.

  • Does it mean that to create multiple static properties, I have to group it together inside companion object block?

    Yes, that's the idiomatic way. Or you can even group them in non-companion objects by their meaning:

    class MyClass {     object IO {         fun makeSomethingWithIO() { /* ... */ }     }      object Factory {         fun createSomething() { /* ... */ }     } } 
  • To instantly create a singleton instance that is scoped to a class, I often write /*...*/ which seems like an unidiomatic way of doing it. What's the better way?

    It depends on what you need in each particular case. Your code suits well for storing state bound to a class which is initialized upon the first call to it.

    If you don't need it to be connected with a class, just use object declaration:

    object Foo {     val something by lazy { ... } } 

    You can also remove lazy { ... } delegation to make the property initialize on first class' usage, just like Java static initializers

    You might also find useful ways of initializing singleton state.

like image 96
hotkey Avatar answered Sep 19 '22 05:09

hotkey