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 mutable variable in Kotlin, declared using the var keyword, is a memory location whose stored value or object reference can be reassigned after its initial declaration. Unlike read-only variables (val), a var permits continuous state mutation throughout its lifecycle within its lexical scope.

Syntax

The standard syntax requires the var keyword, an identifier, an optional explicit type declaration, and an assignment.
var identifier: Type = initialValue
Kotlin’s compiler supports type inference, allowing the omission of the explicit type if the initial value provides sufficient context:
var counter = 0        // Inferred as Int
counter = 1            // Valid reassignment
counter += 1           // Valid reassignment

Technical Characteristics

Strict Static Typing While the value of a var can change, its data type is statically bound at compile time. Reassigning a value of a different, non-conforming type results in a compilation error.
var status = "Active" // Statically typed as String
status = "Inactive"   // Valid reassignment
// status = 404       // Compilation Error: Type mismatch
Underlying JVM Representation The compilation behavior of a var depends heavily on its declaration context:
  • Local Variables: When declared inside a function, a var compiles to a standard, non-final local variable in Java bytecode.
  • Top-level Properties: When declared outside of any class or interface, a var compiles to a private static field accompanied by static getter and setter methods within a generated file-level facade class (e.g., FilenameKt).
  • Class Properties: When declared as a member of a class, a var generates a getter and a setter method. The visibility of these accessors matches the property’s visibility modifier (e.g., a private var generates private accessors). Furthermore, a setter’s visibility can be independently restricted (e.g., using private set on a public property). A private backing field is generated in memory only if at least one accessor uses the default implementation, or if a custom accessor explicitly references the field identifier. Properties with fully custom getters and setters do not generate a backing field.
Custom Accessors Because a var property exposes a mutator (setter), Kotlin allows developers to override the default set() function to intercept and manipulate the reassignment process. The field identifier is used to safely access the generated backing field without triggering recursive setter calls.
class Temperature {
    var celsius: Float = 0.0f
        get() = field
        set(value) {
            require(value >= -273.15f) // Intercepting mutation
            field = value              // Assigning to the backing field
        }
        
    var isFreezing: Boolean
        get() = celsius <= 0.0f
        set(value) {
            celsius = if (value) 0.0f else 1.0f
        } // No backing field generated for isFreezing
}
Late Initialization A mutable variable can bypass immediate initialization using the lateinit modifier. This modifier can be applied to class properties, top-level properties, and local variables. This contract promises the compiler that the var will be assigned a reference before its first memory access, thereby avoiding the need to declare the type as nullable (?). The lateinit modifier is exclusively compatible with var and cannot be applied to primitive types (e.g., Int, Double).
lateinit var globalConfiguration: String // Top-level lateinit var

class Service {
    lateinit var configuration: String   // Class property lateinit var
    
    fun setup() {
        lateinit var tempConfig: String  // Local lateinit var
        
        tempConfig = "Local Init"
        configuration = "Initialized"    // Reassigned before first read
    }
}
Master Kotlin with Deep Grasping Methodology!Learn More