In Java, We can do this:
public class TestA {
public static final boolean flag = true;
public static final String str = flag ? "A" : "B"; // ok
}
但不能在Kotlin
class TestA {
companion object {
const val flag = true
const val str = if (flag) "A" else "B" //err: Const 'val' initializer should be a constant value
val str2 = if (flag) "A" else "B" //ok, but not equals [public static final] in Java.
}
}
Tried @JvmStatic on non-const str2, but decompiled to java code, it's
private static final String str2 = "A"
public static final String getStr2() {
return Companion.getStr2();
}
Problem: kotlin if-else equals ?: in java,but cannnot use for const val. need solution for this.