Reference Java interface static fields in Kotlin

2019-01-26 07:26发布

问题:

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:

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.



标签: kotlin