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
1
vote
1 answer

What difference does it make to have Companion.foo in a companion object?

In Kotlin, if I have a function in a companion object with Companion. as the prefix (Companion.foo), what difference will it make compared to a foo inside the companion object? I noticed that in the following code, Companion.foo will make it…
z11i
  • 951
  • 11
  • 26
1
vote
0 answers

Kotlin Companion Object Limitation about Reflection and Inheritance

Assume: I need a lot of companion (static-like) methods for subclasses of Parent I need a lot of method (non-static) for subclasses of Parent I have more than 50 subclasses of Parent clazz and some other fields must compute per class (not instance)…
Behnam
  • 2,212
  • 2
  • 22
  • 32
1
vote
2 answers

How to call Kotlin companion factory method using callBy()?

I have code accepts a class as a parameter and prepares data to call either the constructor for that class of a companion object factory method if the factory method is present. All works fine when calling the constructor, but I get the error…
innov8
  • 2,093
  • 2
  • 24
  • 31
1
vote
2 answers

Companion object with extension function in kotlin?

I would like to have extension function and use logger from kotlin-logging and have constants inside companion object. My function: fun String.toFoo(): Foo { logger.debug { "Mapping [$this] to Foo" } if(MY_CONST.equals(this) { …
pixel
  • 24,905
  • 36
  • 149
  • 251
1
vote
1 answer

Kotlin Memory allocation behaviour for object class and companion object

Kotlin provide a better way to create singleton object and companion object to access class member via class name. Cool this is !! But how the memory allocation work for both declaration, does this same work like static in java and remain retain…
CoDe
  • 11,056
  • 14
  • 90
  • 197
1
vote
0 answers

in kotlin, what is the right one for the CREATOR in a Parcelable class

with kotlin, having a Data class implements Parcelable seems there are two way of defining the CREATOR: Parcelable in the companion: 1. data class Data ( @SerializedName("id") val id: String?, @SerializedName("blob") val blob:…
lannyf
  • 9,865
  • 12
  • 70
  • 152
0
votes
2 answers

How to get packageName in companion object?

I want to make a unique key when i send a intent/bundle to new Activity/Fragment in Android. So, i decided to use packageName. companion object { val MY_UNIQUE_KEY = "${this@Companion::class.java.packageName}MY_KEY" fun newInstance(user:…
0
votes
0 answers

Does cancelling a coroutine cancel the write to a file?

suspend fun copy(oldFile: File, newFile: File): Boolean{ return withContext(Dispatchers.IO) { var inputStream: InputStream? = null var outputStream: OutputStream? = null try { val fileReader =…
Diken Mhrz
  • 327
  • 2
  • 14
0
votes
1 answer

cant understand this kotlin companion object code

I went through this code example but i can't get it to run neither do understand what it does exactly. data class Order( val id: String, val name: String, val data:String ) data class OrderResponse( val id: String, val name:…
0
votes
2 answers

Kotlin. How to check if value is in range?

I have a field and I need to compare if the given field is equal to one of my constants. I have three constants in total: companion object { private const val NEGATIVE_POSITION = -1 private const val ZERO_POSITION = 0 …
testivanivan
  • 967
  • 13
  • 36
0
votes
0 answers

How to access function of companion object using class object in Kotlin?

In Kotlin, we can access function or property of companion object using class name as following: class DemoClass { companion object { fun someFunction() {} } } // Accessing function DemoClass.someFunction() But how can I access the…
Dhaval
  • 2,724
  • 2
  • 24
  • 33
0
votes
1 answer

Call another function or variable inside a companion object android kotlin from main thread

Please help call another function outside the companion in Kotlin android main activity class MainActivity{ val name = "stackoverflow" companion object(){ //call the both the name and showDialog from this main thread } …
ekibet
  • 176
  • 2
  • 8
0
votes
1 answer

Why can I not access `::class.companionObject`?

I am trying to access the companion object of an unknown class with a known interface, given an instance of the class. Code below: class AccessTest() { companion object { val prop = 5 } fun getComp() { print(this) …
0
votes
2 answers

Is using companion object in activity and application class a good way?

I used a lot of static data in Activities, Adapters, Application, etc with like companion object{ const val SEND_MY_DATA = "sendta" const val SEND_MY_DATA_1 = "sendta1" const val SEND_MY_DATA_2 = "sendta2" } to have common name for…
c-an
  • 3,543
  • 5
  • 35
  • 82
0
votes
3 answers

Why Companion object is not accessible from Java code?

As we know Kotlin and Java are inter-operable. When I try to access Java static variable inside Kotlin code it works, but when I try to access companion object in Java, it does not work.
Saurabh Dhage
  • 1,478
  • 5
  • 17
  • 31