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.

The lateinit modifier in Kotlin allows a non-null property to be declared without an initial value. It defers the initialization of the property to a point after object construction, instructing the compiler to bypass its standard strict null-safety checks which normally require all non-null properties to be initialized at the point of declaration or within the primary constructor.

Syntax

lateinit var propertyName: String

Technical Constraints

To use the lateinit modifier, the property declaration must adhere to the following strict compiler rules:
  • Mutability: The property must be declared as mutable using the var keyword. It cannot be a read-only val.
  • Nullability: The property type must be strictly non-nullable (e.g., String, not String?).
  • Type Restrictions: It cannot be a primitive type (such as Int, Double, Boolean, or Char). This is because lateinit relies on a null reference under the hood at the JVM level to track the uninitialized state, which is structurally impossible for unboxed primitive types.
  • Accessors: The property cannot have custom getters or setters. It must rely on the default backing field accessors.
  • Placement: It can be declared inside a class body, as a top-level property, or as a local variable. It cannot be declared within a primary constructor.

Runtime Behavior

If a lateinit property is accessed before it has been assigned a value in memory, the Kotlin runtime throws a specific exception rather than a standard NullPointerException.
class Example {
    lateinit var text: String

    fun printText() {
        // Throws UninitializedPropertyAccessException if 'text' is not yet assigned
        println(text) 
    }
}

Checking Initialization State

Kotlin provides a reflection-based backing field check to determine if a lateinit property has been initialized. This is accessed using the property reference syntax (::) combined with the .isInitialized extension property.
class Example {
    lateinit var text: String

    fun checkState() {
        if (this::text.isInitialized) {
            println("Initialized with: $text")
        } else {
            println("Property remains uninitialized")
        }
    }
}
Note: The .isInitialized check is lexically restricted because it requires direct access to the property’s backing field. It can only be evaluated on properties within the same class, an outer class, or top-level properties within the same file. Attempting to evaluate .isInitialized on an external object’s property from outside its lexical scope results in a compilation error (Backing field of '...' is not accessible at this point). Furthermore, Kotlin does not support callable references to local variables, so .isInitialized cannot be used on local lateinit variables.
Master Kotlin with Deep Grasping Methodology!Learn More