has Constant
Indicates that the corresponding property has a constant value. On JVM, this flag allows an optimization similarly to KmProperty.hasAnnotations: constant values of properties are written to the bytecode directly, and this flag can be used to avoid reading the value from the bytecode in case it is not there.
Not to be confused with KmProperty.isConst, because const
modifier is applicable only to properties on top-level and inside objects, while property in a regular class can also have constant value. Whether the property has a constant value is ultimately decided by the compiler and its optimizations. Generally, a property initializer that can be computed in compile time is likely to have a constant value written to the bytecode. In the following example, properties a
and b
have hasConstant = true
, while c
has hasConstant = false
:
class X {
val a = 1
val b = a
fun x() = 2
val c = x()
}