← 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.
Syntax
kotlin
class ClassName {
    var property: Type = initialValue
        get() = field              // custom getter
        set(value) { field = value }   // custom setter

    val computed: Type
        get() = expression
}

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
// Define a class `Temperature`
class Temperature(var celsius: Double)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `temp`, set to `Temperature(20.0)`
    val temp = Temperature(20.0)
    // Print "Celsius: ${temp.celsius}" to the console, with a trailing newline
    println("Celsius: ${temp.celsius}")
    temp.celsius = 25.0
    // Print "Updated: ${temp.celsius}" to the console, with a trailing newline
    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
// Define a class `Rectangle`
class Rectangle(val width: Double, val height: Double) {
    val area: Double
        get() = width * height
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `rect`, set to `Rectangle(4.0, 5.0)`
    val rect = Rectangle(4.0, 5.0)
    // Print "Area: ${rect.area}" to the console, with a trailing newline
    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
// Define a class `Account`
class Account {
    // Declare a mutable variable `balance` (a Double), initialized to `0.0`
    var balance: Double = 0.0
        set(value) {
            field = if (value < 0) 0.0 else value
        }
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `account`, set to `Account()`
    val account = Account()
    account.balance = -50.0
    // Print "Balance: ${account.balance}" to the console, with a trailing newline
    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
// Define a class `Counter`
class Counter {
    // Declare a mutable variable `count` (an Int), initialized to `0`
    var count: Int = 0
        private set

    // Define the function `increment` with no parameters
    fun increment() {
        count++
    }
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `counter`, set to `Counter()`
    val counter = Counter()
    counter.increment()
    counter.increment()
    // Print "Count: ${counter.count}" to the console, with a trailing newline
    println("Count: ${counter.count}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
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.