Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Kotlin companion object in Java?

Tags:

java

kotlin

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?

like image 832
Elye Avatar asked Jun 30 '16 10:06

Elye


People also ask

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 do you get the Kotlin companion object?

To create a companion object, you need to add the companion keyword in front of the object declaration. The output of the above code is “ You are calling me :) ” This is all about the companion object in Kotlin. Hope you liked the blog and will use the concept of companion in your Android application.

How do I call Kotlin code from Java?

Getter and Setter Naming getDisplayName(); When we want to access these from Java, we need to explicitly write out the name of the getter. In most cases, the Java name of getters for Kotlin properties is simply get + the property name, as we've seen with User.

What is companion object in Java?

A companion object is initialized when the corresponding class is loaded (resolved), matching the semantics of a Java static initializer. Means that the companion object will be initialized even before calling the constructor of that class as similar to Java static initializer.


2 Answers

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.

like image 195
Marcin Koziński Avatar answered Sep 28 '22 05:09

Marcin Koziński


Ops, I got it. Just use the below.

MainApplication application = MainApplication.Companion.get(mContext); 
like image 23
Elye Avatar answered Sep 28 '22 04:09

Elye