How to access Kotlin companion object in Java?

JavaKotlin

Java Problem Overview


I convert one of my Java class to Kotlin and the class as below.

class MainApplication : Application() {
    companion object {
        operator fun get(context: Context): MainApplication {
            return context.applicationContext as MainApplication
        }
    }
}

It has a static function get.

I still have a Java function accessing it.

MainApplication application = MainApplication.get(mContext);

It was good when MainApplication is in Java. But not when MainApplication in Kotlin, the above code error

Error:(27, 54) error: cannot find symbol method get(Context)

How could I access get in my Java code above?

Java Solutions


Solution 1 - Java

You can add @JvmStatic annotation to the method in companion object to make Kotlin generate a static method.

class MainApplication : Application() {
    companion object {
        @JvmStatic fun get(context: Context): MainApplication {
            return context.applicationContext as MainApplication
        }
    }
}

you can then access it from Java like before converting to Kotlin:

MainApplication application = MainApplication.get(mContext);

EDIT: I feel obliged to add something I learned recently: @JvmStatic doesn't actually move where the method gets generated. It duplicates it, by generating a static method for Java in addition to the method on the companion object. Personally I think this isn't great and it can have some implications depending on a use case, so something worth knowing.

Solution 2 - Java

Ops, I got it. Just use the below.

MainApplication application = MainApplication.Companion.get(mContext);

Solution 3 - Java

By omitting the name of your companion object, the name Companion must be used to access the methods.

Example:

class MyClass1 {
    companion object Object1 {
        fun method1 {
        }
    }
}

class MyClass2 {
    companion object {
        fun method2 {
        }
    }
}

To invoke the first companion object method you would do the following:

MyClass1.method1()

To invoke the second:

MyClass2.Companion.method2()

See the Kotlin docs on Companion Objects for details.

Solution 4 - Java

You may encounter a problem where you cannot access the Companion object's method in Java if the new keyword is used in the method call. The new keyword should be omitted. The documentation states:

> Companion objects and their members can only be accessed via the containing class name, not via instances of the containing class.

So if you have a class like this:

class MyClass {
    companion object {
        fun create() {}
    }
}

You can call the companion object's method like this:

MyClass.create()

But not like this:

new MyClass.create

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElyeView Question on Stackoverflow
Solution 1 - JavaMarcin KozińskiView Answer on Stackoverflow
Solution 2 - JavaElyeView Answer on Stackoverflow
Solution 3 - JavaMikeView Answer on Stackoverflow
Solution 4 - JavaDmitriy PavlukhinView Answer on Stackoverflow