Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 public to all the elements (class, object and val), but it has no effect.

How can I access these companion object constants?

Update I think my question is different from the given duplicate, because I want to create constants, instead of a static method.

like image 785
Bart Friederichs Avatar asked Mar 23 '19 17:03

Bart Friederichs


People also ask

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 String myString = "Logged in as " + user. 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.

Can you use Kotlin in Java?

 Yes. Kotlin is supported as a first-class language on Android.

How to invoke companion object methods in Kotlin?

By omitting the name of your companion object, the name Companion must be used to access the methods. To invoke the first companion object method you would do the following: See the Kotlin docs on Companion Objects for details. Show activity on this post.

What is the visibility of a Kotlin property setter?

The visibility of the field will be the same as the visibility of lateinit property setter. Kotlin properties declared in a named object or a companion object will have static backing fields either in that named object or in the class containing the companion object.

Why can't I access companion object's method in 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:

Can Kotlin code be called from Java?

Kotlin code can be easily called from Java. For example, instances of a Kotlin class can be seamlessly created and operated in Java methods. However, there are certain differences between Java and Kotlin that require attention when integrating Kotlin code into Java.


1 Answers

I added const, and everything was fine:

class Storage {
    companion object {
        const val COL_ID = "id"
    }
}
like image 116
Bart Friederichs Avatar answered Oct 27 '22 01:10

Bart Friederichs