liveBook · Manning

Mutable property

As explained in Kotlin in Action, 2E

A mutable property is a Kotlin property whose value can be changed after initialization.

A mutable property is a Kotlin property declared with var, so its value can change after initialization. Kotlin properties replace the usual field-plus-accessor pattern: var provides a getter and setter, while val is read-only. section 2.2.1

A student status that changes

class Person( val name: String, var isStudent: Boolean ) fun main() { val person = Person("Bob", true) println(person.isStudent) // true person.isStudent = false // Graduation! println(person.isStudent) // false } The object reference is val, but its isStudent property is var, so the property can be updated. Kotlin property syntax hides the getter and setter. section 2.2.1

Listing 2.4 Declaring a mutable property in a class
class Person(
    val name: String,         #1
    var isStudent: Boolean    #2
)

Inspect the compact class declaration that pairs a read-only name with a mutable isStudent property. section 2.2.1

A mutable property normally stores its value in a backing field. Its setter can add validation, notifications, logging, or other update logic before assigning through field. section 2.2.1 section 4.2.4

Logging changes with a custom setter

class User(val name: String) { var address: String = "unspecified" set(value) { println("$field -> $value") field = value } } user.address = "new value" invokes the setter, which can inspect the previous value through field and then store the new value. section 4.2.4

Listing 4.20 Accessing the backing field in a setter
class User(val name: String) {
    var address: String = "unspecified"
        set(value: String) {
            println(
                """
                Address was changed for $name:
                "$field" -> "$value".           #1
                """.trimIndent()
            )
            field = value                       #2
        }
}
 
fun main() {
    val user = User("Alice")
    user.address = "Christoph-Rapparini-Bogen 23"
    // Address was changed for Alice:
    // "unspecified" -> "Christoph-Rapparini-Bogen 23".
}

Inspect a complete setter that logs an address change and updates the backing field. section 4.2.4

Kotlin exposes the same property behavior to Java through accessor methods: Java can read name with getName, read isStudent with isStudent, and update it with setStudent. section 2.2.1

How Kotlin in Action, 2E covers it

How deep it goes
Explains declarations, generated accessors, custom setters, backing fields, and field-free computed properties.
Examples
Uses Person.isStudent for graduation and User.address for logging changes.
What you can do after
Build it from scratch by declaring var properties, updating them, and adding setter logic with field.
How it connects
Connects mutable properties with getters, setters, backing fields, computed properties, classes, and Java interoperability.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.