Questions tagged [kotlin-companion]

Kotlin Companion

The Kotlin Companion keyword is used to indicate that a particular class has a companion object. A companion object is an object that is defined inside a class and has the same name as the class, but with a companion keyword in front of it.

Companion objects are used for several purposes, such as:

  • They can contain factory methods for creating instances of the class.
  • They can provide a singleton instance of the class.
  • They can contain additional utility methods related to the class.

Here is an example of a class with a companion object:

class MyClass {
    companion object {
        fun create(): MyClass {
            // factory method
            return MyClass()
        }
    }
}

val myObject = MyClass.create()
39 questions
190
votes
5 answers

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…
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
107
votes
5 answers

Why and when to use @JvmStatic with companion objects?

I'm trying to understand the difference between using/not using @JvmStatic, and when I should use either one. So, with Kotlin and Java, I can do this: TestKotlin.kt class TestKotlin { companion object { val someString = "hello world" …
TooManyEduardos
  • 4,206
  • 7
  • 35
  • 66
26
votes
4 answers

What is the point of naming a companion object in kotlin

The documentation for companion objects has the following example class MyClass { companion object Factory { fun create(): MyClass = MyClass() } } Here Factory is the name of the companion object. It then goes on to say: The name…
Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
6
votes
1 answer

How to access Kotlin companion objects from Java

I have this Kotlin class: class Storage { companion object { val COL_ID = "id" } } and I want to use the COL_ID in my Java code: doSomething(Storage.COL_ID); but, the compiler tells me that COL_ID is private. I have tried to add…
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
5
votes
1 answer

how to access outer class' javaClass.simpleName from companion object in kotlin?

I would like to be able to access the simpleName of my class from it's companion object. I would like this: val o1 = Outer("foo") val o2 = Outer("bar") to print the following output: Outer: hello Outer: foo Outer: bar The actual use case would be…
Gavriel
  • 18,880
  • 12
  • 68
  • 105
4
votes
1 answer

Kotlin - What does companion object fun do?

Declaring a "static" function in Kotlin is done using: companion object { fun classFoo() { //do something } } However I was mistakenly coding companion object fun classFoo() { //do something } Expecting the code to do the…
htafoya
  • 18,261
  • 11
  • 80
  • 104
3
votes
3 answers

Companion objects benefits of posibility to implement interfaces

Why in Kotlin/Scala companion objects can implements some interfaces, what benefits this can have? When is useful to use this feature?
Axel
  • 125
  • 1
  • 12
2
votes
2 answers

object instances in companion objects

This is a question about language design rather than trying to solve a specific problem. I noted two oddities about object instances inside companion objects: object instances can't be referenced without the .Companion object instances cannot be…
Anm
  • 3,291
  • 2
  • 29
  • 40
2
votes
2 answers

Is it possible to extends a class that has a non empty constructor with a companion object

I have a code in Java that i want to change to Kotlin syntax. The jave code is: public class CountryDataItem (String countryNane,String countryUrl) { public static RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent) { new…
Eitanos30
  • 1,331
  • 11
  • 19
2
votes
3 answers

Accessing a Kotlin object nested in companion object from Java

I have a structure like this in Kotlin companion object Constants { /** * Collection of fields and values relative to phone books. */ object PhoneBooks { /** * Field indicating the ID of a phone book. *…
Lore
  • 1,286
  • 1
  • 22
  • 57
2
votes
2 answers

Kotlin: cannot use const val from companion object in an outer enum class

I have a Kotlin enum class defined like this: enum class EnumClass(val string: String) { VALUE_A(A), // [1] VALUE_B(B); // [2] companion object { const val A = "A" const val B = "B" } } and the compiler gives me…
goshki
  • 120
  • 9
2
votes
1 answer

Memory leak in Kotlin companion objects?

I'm using Kotlin for Android development. Interesting question. In Java saving Context in static field is a memory leak. But if I'm storing context in Kotlin companion object, Android Studio doesn't say that it's a memory leak. It's means that in…
Vladimir Fisher
  • 3,090
  • 2
  • 17
  • 23
1
vote
1 answer

Can I add companion extension without first having companion object within a class?

For the below code, I can add invoke extension to the Companion operator fun MyValue.Companion.invoke(value: Int) = MyValue(value.toString()) class MyValue(private val value: String) { companion object fun print() = println("value =…
Elye
  • 53,639
  • 54
  • 212
  • 474
1
vote
1 answer

Variable in base class is null when initialized from derived class but called from super

I'm having trouble converting an inherited singleton pattern from Java to Kotlin. open class ThumbnailManager { lateinit var data: Data companion object{ fun getInstance(): ThumbnailManager{ return ThumbnailManager() …
galaxigirl
  • 2,390
  • 2
  • 20
  • 29
1
vote
0 answers

Why does companion object put const vals into parent class?

I have a class in Kotlin: class AClass { companion object { const val CONST_VAL = "THIS IS A CONST VAL STRING" val JUST_VAL = "THIS IS A NON-CONST VAL STRING" fun aFunction() {} } } and a Main class in Java which is…
1
2 3