3

I have a project that is written with both Java and Kotlin languages and recently I faced next issue.

Let's say we have MyMonth enum:

public enum MyMonth {
    JAN("January"),
    FEB("February");

    private final String name;

    MyMonth(final String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Then in Kotlin when we print the name of the month:

fun main() {
    val month = MyMonth.JAN
    println(month.name)
}

we get:

JAN

which is what is described in the documentation https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-enum/name.html, however actually the name should be January.

Is there a way to get a name in Kotlin that is specified in Java enum?

UPD: My Kotlin version is 1.3.30-release-170

UPD2: IDEA even shows me that name is coming from the getName() method defined in Java: enter image description here

UPD3: When we explicitly use .getName() it works, however it looks kind of weird

2
  • Shouldn't you call MyMonth.JAN.name instead? Commented Nov 26, 2019 at 17:41
  • My guess is since Kotlin enums already have a name property, as seen here, there's a conflict between the Java getter and the Kotlin property. If possible, I recommend changing the name of the Java field and getter to something other than name and getName(). Personally, I think it's a bad idea to have a field named name in a Java enum anyway, considering the existence of the name() method. If you can't change it, what if you call getName() explicitly in the Kotlin code? Commented Nov 26, 2019 at 18:02

3 Answers 3

5

You can call the getter directly instead of using the property syntax:

fun main() {
    val name = MyMonth.JAN.getName()
    println(name)
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Orest that's because name is reserved in enums. If you could pick another property name, then it would be non-excessive
@Orest you could make an extension property which calls getName() to make it look more idiomatic
@NikolaiShevchenko yes, it's clear to me, however I already mentioned that in the comments that it's not that rare when you have to use an enum that comes from Java with name field and you're not able to change it
3

Your Java API has name as a private field. Nothing can access it, not in Java nor Kotlin.

If you want to access it, add e.g. the following to the Java API:

public String getMonthName() { return name; }

...and then access it from Kotlin as

val month = MyMonth.JAN.monthName

4 Comments

In Java I can simply add a getter and call MyMonth.JAN.getName() which would return me what I need. But this getter is not used in Kotlin even though IDEA show me that it's from there, I'll update my question to make it more clear.
Hey @Slaw that's a good suggestion, unfortunately it's not possible to rename that field as I'm not the owner of the library that has this enum. Also, I think it's not that rare when you have a Java enum that has field name in it.
name is specifically complicated by being already baked into the definition for enums. You may just have to call getName() explicitly and not use it as a property, even if IntelliJ tells you you don't have to. It may be outright lying.
Yes I understand that name is baked into Kotlin enum, just noticed this weird behavior and was confused for a while. Thanks!
0

enum is now low usage. You can use IntDef instead of enums.

@IntDef(MyMonth.JAN, MyMonth.FEB)
@Retention(AnnotationRetention.SOURCE)
annotation class MyMonth  {
    companion object {
        const val JAN = 0
        const val FEB = 1

        fun getDisplayString(toDoFilterTypes: Int): String {
            return when (toDoFilterTypes) {
                JAN -> "All"
                FEB -> "Job"
                else -> "N/A"
            }
        }
    }
}

1 Comment

This seems to be outdated now - medium.com/default-to-open/…

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.