What does it mean to set a mutable object to 'val'?

scala

Solution

It's still a `val`. The reference can't be changed, but the object referred to can have its internal state mutated.

`val` means you can't do this reassignment:

val st = new Pojo()
st = new Pojo()      // invalid!

For this you need a `var`:

var st = new Pojo()
st = new Pojo()      // ok

Problem

I'm setting a java Pojo instance variable to 'val' & changing its state after it's initialized. Will this cause any issues since its really a 'var' ? ``` val st = new Pojo(); st.setInt(0); ```

Original source

Related problems