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
0
votes
1 answer

How to override inherited getter from java class in a kotlin companion object

First of all, Manager (a Kotlin class) inherits from JavaPlugin (which is a Java class). This JavaPlugin class contains a getter method getPlugin(Class). Manager class: class Manager : JavaPlugin() { companion object { [more…
Jakob
  • 141
  • 1
  • 13
0
votes
1 answer

Companion object method not accessible from subclass

I'm trying to use the method getInstance from a subclass. Any idea why the mentioned method is unaccessible and how to change this code so i can access it? For the curious mind, this example structure comes from trying to subclass a Room database…
AndrewBloom
  • 2,171
  • 20
  • 30
0
votes
1 answer

Global logger in Kotlin

I am currently working on a relatively large project in Kotlin. I would like to implement a logging method, however for many reasons (notably because I need a very specific type of clock management and a wide variety of appenders which will be a…
Ben
  • 317
  • 3
  • 12
0
votes
1 answer

Is a Kotlin Companion Object just a way to add functions from an interface?

I am reading the Kotlin in Action book and trying to understand Companion Objects better, are there any other uses for Companion Ojbects other than adding method implementations from an interface/abstract class? I came across a way of instantiating…
David Aleksanyan
  • 2,953
  • 4
  • 29
  • 39
0
votes
0 answers

Instance in companion object

I can't get on with access on the var mBleWrapper: BleWrapper? = null in other activities. There is a predefined Java interface called BleWrapperUiCallbacks, which I suspect contains the functionality I need and the Java-Class BleWrapper. I…
0
votes
1 answer

Kotlin set variable and call method in main class from companion object

I'm new to Kotlin, and I don't understand if/how I can call a function or set a variable from the companion object: class MyClass { public var myVar: Boolean public fun myFunc(): Int { ... } companion object { private fun…
ocramot
  • 1,361
  • 28
  • 55
0
votes
2 answers

Initialize ImageView in companion Object Kotlin

I am trying to initialize an ImageView in companion object as I want the methods to be static but I have an error is there an alternative approach below is my code companion object { lateinit var bufferingAnimation : LoadingAnimation var…
vinay kumar
  • 555
  • 1
  • 6
  • 16
0
votes
1 answer

Kotlin static constant field returning modified String value

I'm trying to reach a constant String value of the Class name. But I really don't understand why I get a modified String value. Here is the code that I'm working on: class TestClass { companion object { @JvmField val…
Orcun Sevsay
  • 1,310
  • 1
  • 14
  • 44
0
votes
1 answer

How to keep the reference of the singleton class object in the companion object, Kotlin

The code that I want to convert is the following; public class AndroidLauncher extends AndroidApplication { public static AndroidLauncher androidLauncher; @Override protected void onCreate (Bundle savedInstanceState) { …
Efe Budak
  • 659
  • 5
  • 27
1 2
3