As Java returns by value (i.e. object references are returned from methods), if for example I return a String like so:
private String myString = "foo";
public String getString() {
return this.myString;
}
and the String class wasn't immutable, then the caller of getString()
could modify myString
, which may not be the desired behaviour; other parts of the system might not want or expect myString
to change. So the caller can only change the object which myString
points to, not myString
itself.