class Person(val name:String,var age:Int )
def person = new Person("Kumar",12)
person.age = 20
println(person.age)
These lines of code outputs 12
, even though person.age=20
was successfully executed. I found that this happens because I used def in def person = new Person("Kumar",12)
. If I use var or val the output is 20
. I understand the default is val in scala. This:
def age = 30
age = 45
...gives a compilation error because it is a val by default. Why do the first set of lines above not work properly, and yet also don't error?