liveBook · Manning

Read-only property

As explained in Kotlin in Action, 2E

A read-only property is a Kotlin property that can be accessed but not reassigned through its public declaration.

A read-only property is declared with Kotlin’s `val` keyword. Code can read its value but cannot reassign it through the property declaration. Kotlin properties replace the usual field-and-accessor pattern with a compact, idiomatic syntax. section 2.2.1

A read-only property beside a mutable property

```kotlin class Person( val name: String, var isStudent: Boolean ) ``` `name` is read-only, while `isStudent` can be changed after construction: ```kotlin val person = Person("Bob", true) println(person.name) // Bob person.isStudent = false // allowed ``` The declaration of `name` provides a getter but no setter. Kotlin still exposes that property to Java as `getName()`, while the `isStudent` property is exposed through `isStudent()` and `setStudent(...)`. section 2.2.1

A `val` property is read-only through its public declaration; it does not necessarily mean that every related implementation detail is immutable. By default, Kotlin generates storage and a getter, and custom accessors can compute a property using different logic. section 2.2.1

How Kotlin in Action, 2E covers it

How deep it goes
Explains declaration syntax, generated getters, custom accessors, storage, and Java naming, but does not develop advanced accessor mechanics.
Examples
Shows `Person.name`, `Person.age`, and a Kotlin `Person` instance read alongside mutable `isStudent`.
What it assumes
Teaches the distinction between `val` and `var` while assuming only basic Kotlin class and constructor syntax.
What you can do after
Apply `val` to declare readable Kotlin properties and recognize their generated Java getter representation.
How it connects
Connects read-only properties with mutable properties, fields, getters, setters, data 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.