Custom getter/setter for properties created by type params

If you need a custom getter or setter for your property, you cannot declare it in the primary constructor parameter. You need to declare a property in the body of the class.

class Person(val name: String, age: Int) {
  var age: Int = age
  set(value) {
  println(“Setting age to $value”)
  field = value;
  }
}

5 Likes