6

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.

9
  • You are missing const from your const val COL_ID (or @JvmField) Commented Mar 23, 2019 at 17:17
  • @EpicPandaForce that simple ... Commented Mar 23, 2019 at 17:17
  • Possible duplicate of How to access Kotlin companion object in Java? Commented Mar 23, 2019 at 17:37
  • @Steve no, it finds it, it just was not accesible. Commented Mar 23, 2019 at 17:40
  • Hmmm...I wouldn't think that would work. Ok, well, I don't understand it then. So what was the solution you ultimately came up with? You said you already tried making it 'public' Commented Mar 23, 2019 at 17:41

1 Answer 1

5

I added const, and everything was fine:

class Storage {
    companion object {
        const val COL_ID = "id"
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.