noktigula noktigula - 1 year ago 329
Java Question

Kotlin for android and static final inner classes

I'm trying to rewrite my android app on Kotlin, and I've faces problem while converting ContentProvider contract class.
So I have simple contract class (default stuff like content type and content uris is omitted):

public final class Contract {
public static final class Contacts {
public static final String NAME = "Name"
public static final String BIRTH = "Birth"
public static final String IMAGE = "Image"

If I understand correctly, in Kotlin we don't have static members. Instead of this, we have "companion objects". So after converting it to Kotlin, I have this code:

object BirthdayContract {
class Contacts : BaseColumns {
companion object {
val NAME = "Name"
val BIRTH = "Birth"
val IMAGE = "Image"

But when I'm trying to access fields like Contract.Contacts.NAME, I have error: "NAME has private access". Changing the visibility modifiers gave no effect.
So is there some way to use such contract classes in Kotlin, or it better to keep it in Java?

I think that I should clarify - my caller code is also in Kotlin.
The problem was in @JvmField annotation. With it, I can access static members directly, without calling getters.

Answer Source

NAME is a property and by default its backing field has private access. But since it's a property, there's a getter for it: getNAME().

Since this is not how you naturally access constants in Java, there are a few ways to directly expose the field:

  1. const: const val NAME = "Name"
  2. @JvmStatic annotation: @JvmStatic val NAME = "Name"
  3. @JvmField: @JvmField val NAME = "Name"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download