Bart Friederichs
Bart Friederichs

Reputation: 33511

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.

Upvotes: 6

Views: 1704

Answers (1)

Bart Friederichs
Bart Friederichs

Reputation: 33511

I added const, and everything was fine:

class Storage {
    companion object {
        const val COL_ID = "id"
    }
}

Upvotes: 5

Related Questions