Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A 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 the const val modifiers. The Kotlin compiler enforces strict structural and typing rules for const properties:
  1. Placement: It must be declared at the top level of a file, as a member of an object declaration, or within a companion object. It cannot be a standard class property or a local variable.
  2. Type Restriction: The property type must be a String or a primitive type (Int, Long, Double, Float, Boolean, Char, Byte, Short).
  3. 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.
  4. No Custom Getters: It cannot have a custom getter implementation, as the value must be statically determinable at compile time.

Syntax

// 1. Top-level declaration with visibility modifiers
private const val MAX_CONNECTIONS: Int = 10
const val DEPRECATION_REASON: String = "Use V2 API instead"

// Annotation argument capability (invalid with standard 'val')
@Deprecated(DEPRECATION_REASON)
fun legacyFunction() {}

class DatabaseClient {
    // 2. Companion object declaration
    companion object {
        internal const val TIMEOUT_MS: Long = 5000L
    }
}

// 3. Object declaration (Singleton)
object AppConfig {
    const val VERSION_CODE: Int = 1
}

Compilation Behavior and Bytecode

Understanding the distinction between val and const val requires examining the generated Java bytecode and compiler optimizations. Standard val:
val runtimeConstant = 100
The compiler generates a 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:
public const val COMPILE_CONSTANT = 100
The compiler generates a 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:
val total = COMPILE_CONSTANT + 50
The Kotlin compiler evaluates constant expressions at compile time. The generated bytecode directly assigns the pre-computed value, completely eliminating both the property reference and the addition operation from the compiled output:
// Equivalent Java bytecode representation
int total = 150;
This strips the call site of method dispatch overhead and allows the JVM to perform further static optimizations.
Master Kotlin with Deep Grasping Methodology!Learn More