Skip to content
Justice Tech School

Properties, Getters and Setters

Properties can have custom get and set blocks. field refers to the backing value. lateinit delays initialisation of non null vars.

Example
class Temp { var c = 0.0
    val f get() = c * 9 / 5 + 32
    var name = ""
        set(v) { field = v.trim() } }
val t = Temp(); t.c = 100.0; t.name = "  Ada "
println("${t.f} [${t.name}]")

Exercise

Backing value keyword.

set(v) {  = v }

Quiz Test

What is field?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.