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 custom setter in Kotlin is an explicit implementation of the set accessor for a mutable (var) property, allowing developers to intercept and define the exact execution logic that occurs during property assignment. By default, Kotlin generates implicit getters and setters for properties, but defining a custom setter overrides the default assignment mechanism.

Syntax

The custom setter is declared immediately following the property declaration using the set keyword. It takes a single parameter representing the incoming value.
var greeting: String = "Hello"
    set(value) {
        // Custom execution logic
        field = value
    }

The Backing Field (field)

To store a value in memory, Kotlin provides an implicit backing field accessed via the field identifier. Inside the custom setter, the incoming value must be assigned to field rather than the property name itself. Assigning directly to the property name within its own setter triggers a recursive invocation of the setter, resulting in a StackOverflowError.
var data: String = "Initial"
    set(value) {
        // CORRECT: Assigns to the implicit backing field
        field = value 
        
        // INCORRECT: Causes infinite recursion
        // data = value 
    }
Note: A property in Kotlin has a backing field if it uses the default implementation of at least one of the accessors, or if a custom accessor references it through the field identifier. If a custom setter does not reference field, the property will still have a backing field unless a custom getter is also explicitly defined and also does not reference field. Omitting the getter entirely generates a default getter that implicitly requires a backing field. A critical language rule dictates that a property without a backing field cannot have an initializer.

Parameter Naming and Type

By convention, the setter parameter is named value, but it can be arbitrarily renamed. The parameter’s type is inferred from the property declaration. While optional, the type can be explicitly declared in the setter signature, provided it exactly matches the property’s declared type.
var count: Int = 0
    set(newValue: Int) { // Parameter renamed and type explicitly declared
        field = newValue
    }

Visibility Modifiers and Annotations

The visibility of a custom setter can be modified independently of the property, provided the setter’s visibility is equal to or more restrictive than the property’s visibility. Annotations can also be applied directly to the set keyword.
var internalState: String = "Default"
    private set(value) {
        field = value
    }
If only the visibility or annotations need to be changed without altering the default assignment logic, the setter body can be omitted entirely. Built-in annotations (like @Suppress) or properly imported external annotations can be applied directly to the modifier:
var restrictedProperty: Int = 0
    @Suppress("unused") private set
Master Kotlin with Deep Grasping Methodology!Learn More