Q:

access modifers in scala with var and val

I am getting confused in use of var and val in access modifers,
name is var , therefore we can change it, it is ok.
But p is val, how we are able to change the
p.name = "Fred Flintstone"
scala> class Person(var name: String)
defined class Person

scala> val p = new Person("Alvin Alexander")
p: Person = Person@139b7bb

scala> p.name
res0: String = Alvin Alexander

scala> p.name = "Fred Flintstone"
p.name: String = Fred Flintstone

Thanks,

A:

You should remember that variables are just references. So when you create variable with val it means that you can't assign new reference to it. In var case its possible. 
As you see when you create val p = new Person("Alvin Alexander") p will have reference to same Person object always. You can't create another Person and assign it to p 
But any person instance has var name variable which is reassignable to new strings.
As a result final p has reference to mutable Person instance. 

