> ## 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.

# Kotlin Const Property

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

```kotlin theme={"dark"}
// 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`:**

```kotlin theme={"dark"}
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`:**

```kotlin theme={"dark"}
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:

```kotlin theme={"dark"}
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:

```java theme={"dark"}
// 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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
