What can you do to make an object such as this immutable? I am mostly concerned about solving public void someMethod(SomeObject someObject) { } <== This case
For example:
public class SomeObject {
private String someString;
public void setSomeString(String someString) {
this.someString = someString;
}
public void getSomeString() {
return someString;
}
}
public void someMethod() {
final SomeObject someObject = new SomeObject();
someObject.set("Want to prevent this"); // <-- SomeObject is mutable in this case
}
public void someMethod(SomeObject someObject) {
someObject.set("And prevent this!"); // <-- And so is this
}
finalshould be reviewed (it has nothing to do with "single reference").finalhas different meanings, depending on where it is applied (it does not relate to immutability when applied to a class; nor to the immutability of objects named by variables). I have no idea what is meant by "final .. on an interface" (although this question might be an interesting read).final SomeObject someObjectthe reference variablesomeObjectis final and not the object it points to. IMO reference variable cannot point to another object but object can be modified.