← Back to Kotlin Course | Chapter 6: Classes & Objects | Lesson 3 of 7

Properties

Properties are the labeled bits of information a class carries around, and Kotlin quietly builds the getter and setter for you.

Automatic Getters and Setters

Every val/var property automatically has a getter (and, for var, a setter) generated by the compiler, so you access it directly with object.property instead of calling a method.

Example: Automatic Getters and Setters

markup
class Temperature(var celsius: Double)

fun main() {
    val temp = Temperature(20.0)
    println("Celsius: ${temp.celsius}")
    temp.celsius = 25.0
    println("Updated: ${temp.celsius}")
}

Custom Getters

A property can define a custom get() that computes its value from other properties on the fly, rather than storing that value directly.

Example: Custom Getters

markup
class Rectangle(val width: Double, val height: Double) {
    val area: Double
        get() = width * height
}

fun main() {
    val rect = Rectangle(4.0, 5.0)
    println("Area: ${rect.area}")
}

Custom Setters

A var property can define a custom set(value) that runs validation or transformation logic every time a new value is assigned.

Note: Inside a custom setter, field refers to the property's backing storage.

Example: Custom Setters

markup
class Account {
    var balance: Double = 0.0
        set(value) {
            field = if (value < 0) 0.0 else value
        }
}

fun main() {
    val account = Account()
    account.balance = -50.0
    println("Balance: ${account.balance}")
}

Restricting Setter Visibility

Adding private set after a var property lets other code read it freely while only the class itself can change its value, which is a common way to expose read-only state safely.

Example: Restricting Setter Visibility

markup
class Counter {
    var count: Int = 0
        private set

    fun increment() {
        count++
    }
}

fun main() {
    val counter = Counter()
    counter.increment()
    counter.increment()
    println("Count: ${counter.count}")
}
Common Mistakes
  1. Writing manual getX()/setX() methods out of Java habit, when Kotlin properties already generate them automatically.
  2. Forgetting that a custom getter recomputes its value every time it's accessed, rather than being cached automatically.
  3. Using var for a property that should never change from outside the class, instead of val with a private setter or backing field.
Chapter Summary
  • A property declared with val/var automatically gets a generated getter (and setter for var) behind the scenes.
  • Custom getters and setters can be written with get() = ... and set(value) { ... } right after the property declaration.
  • A custom getter re-evaluates its expression every time the property is read; it is not cached.
  • private set on a var property allows public reads but restricts writes to inside the class.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.