11

Can I reference Java interface fields from Kotlin? I have this Java interface:

public interface BaseColumns {
    public static final String _ID = "_id";
    public static final String _COUNT = "_count";
}

And I implement it in Kotlin:

object UserEntry : BaseColumns {
    // some code
}

I get Unresolved reference when I try UserEntry._ID. How can I access the _ID? Am I missing something? Thanks!

1 Answer 1

14

In Kotlin, unlike Java, static members of interfaces are not derived and cannot be called in subclasses without qualifying the interface name.

You should reference _ID through BaseColumns: BaseColumns._ID will work.

This seems to be different for classes: non-qualified name of a base class static member resolves to it, but the member is still not inherited.

Sign up to request clarification or add additional context in comments.

1 Comment

Follow up question: in this particular case there then is no reason to implement the BaseColumns interface right? As it only contains two static member variables?

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.