Scala Setters and Getters

Scala has some baked in sugared syntax to enable getters and setters which look like the use of variables.

private var _age = 0

def age = _age

def age_=(value:Int): Unit = {
_age = value
}

Getter Use:

println(obj)

Setter Use:

obj.age = 18
Show your support

Clapping shows how much you appreciated Laura Dietz’s story.