ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
const property in Kotlin is a compile-time constant, meaning its value is resolved and directly inlined into the calling code during compilation. Unlike a standard read-only val property—which is evaluated at runtime and typically accessed via a generated getter method—a const val replaces all property references with the literal value in the generated JVM bytecode. Because their values are strictly guaranteed at compile time, const properties possess the unique capability of being used as arguments in annotations (e.g., @Deprecated(MESSAGE)), a context where standard val properties are strictly prohibited.
Declaration Requirements
To declare a compile-time constant, the property must be prefixed with theconst val modifiers. The Kotlin compiler enforces strict structural and typing rules for const properties:
- Placement: It must be declared at the top level of a file, as a member of an
objectdeclaration, or within acompanion object. It cannot be a standard class property or a local variable. - Type Restriction: The property type must be a
Stringor a primitive type (Int,Long,Double,Float,Boolean,Char,Byte,Short). - Initialization: It must be initialized immediately with a literal value or another compile-time constant. It cannot be initialized by a function call that requires runtime execution.
- No Custom Getters: It cannot have a custom getter implementation, as the value must be statically determinable at compile time.
Syntax
Compilation Behavior and Bytecode
Understanding the distinction betweenval and const val requires examining the generated Java bytecode and compiler optimizations.
Standard val:
private final backing field and a getter method (getRuntimeConstant()). Accessing this property incurs the overhead of a method invocation at runtime.
Compile-time const val:
static final field that strictly respects the declared Kotlin visibility modifiers. A private const val generates a private static final field, while a public one generates a public static final field.
More importantly, wherever COMPILE_CONSTANT is referenced, the compiler performs constant propagation and constant folding.
If you write:
Master Kotlin with Deep Grasping Methodology!Learn More





