> ## 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 Read-Only Variable

A read-only variable in Kotlin is declared using the `val` (value) keyword and represents a reference that can be assigned exactly once. Once initialized, the variable's reference is locked, and any subsequent attempt to reassign it will result in a compilation error.

```kotlin theme={"dark"}
val language: String = "Kotlin"
// language = "Java" // Compilation error: Val cannot be reassigned
```

## Type Inference

If the variable is initialized at the point of declaration, the Kotlin compiler infers the type, making the explicit type declaration optional.

```kotlin theme={"dark"}
val version = 1.9 // Type inferred as Double
```

## Deferred Initialization

Immediate assignment is not strictly required. A read-only variable can be declared without an initial value, provided the compiler can statically verify that it is initialized exactly once across all possible execution paths before its first read.

```kotlin theme={"dark"}
val status: Int
val condition = true

if (condition) {
    status = 200
} else {
    status = 404
}
// status is guaranteed to be initialized exactly once before being accessed
```

## Read-Only vs. Immutability

The `val` keyword enforces a read-only reference, not deep object immutability. If a `val` holds a reference to a mutable object, the reference itself cannot be changed to point to a new object in memory, but the internal state of the referenced object can still be mutated.

```kotlin theme={"dark"}
val items = mutableListOf("A", "B")
items.add("C") // Valid: Mutating the internal state of the referenced object
// items = mutableListOf("D") // Invalid: Attempting to reassign the reference
```

## Properties and Custom Getters

When declared as a class-level or interface-level property, `val` instructs the compiler to generate a getter method but no setter method. Because a `val` property can utilize a custom getter, its returned value is not guaranteed to be constant across multiple invocations. It lacks a backing field if a custom getter is provided without referencing `field`.

```kotlin theme={"dark"}
class SystemInfo {
    val timestamp: Long
        get() = System.currentTimeMillis() // Returns a different value on each access
}
```

In this scenario, `timestamp` is read-only (it cannot be assigned a value via a setter), but its evaluated result is dynamic, demonstrating that `val` dictates assignment restrictions rather than strict state immutability.

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