access modifers in scala with var and val

前端 未结 2 569
陌清茗
陌清茗 2021-01-28 05:26

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 F

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-28 06:25

    val or var works only for next variable (on it's reference), it does not indicate whether variable is mutable or not. In your example, p is mutable (because one can change p.name). But you cannot write p = anotherPerson.

提交回复
热议问题