-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
const
it only works with primitives and Strings:
class Constants {
companion object {
// won't compile
const val FOO = Foo()
}
}
- accesses to a
const
val get inlined by the compiler, while@JvmFields
don't
class Constants {
companion object {
@JvmField val FOO = Foo()
}
}
Let's take the following code:
fun main(args: Array<String>) {
println(Constants.FOO)
}
Here's what we get with @JvmField val FOO = Foo():
public final class MainKt {
public static final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
Foo var1 = Constants.FOO;
System.out.println(var1);
}
}
And with const val FOO = "foo":
public final class MainKt {
public static final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
String var1 = "foo";
System.out.println(var1);
}
}
There's no call to Constants.FOO
in the second example, the value has been inlined.