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!

pt2121
  • 11,720
  • 8
  • 52
  • 69

1 Answers1

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.

hotkey
  • 140,743
  • 39
  • 371
  • 326
  • 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? – Joris Nov 24 '17 at 23:01