Let's say I have a class Foo in Java that has immutable data:
class Foo {
final private int x;
public int getX() { return this.x; }
final private OtherStuff otherstuff;
public Foo(int x, OtherStuff otherstuff) {
this.x = x;
this.otherstuff = otherstuff;
}
// lots of other stuff...
}
Now I'd like to add a utility method that creates a "sibling" value with identical state but with a new value of x. I could call it setX():
class Foo
{
...
Foo setX(int newX) { return new Foo(newX, this.otherstuff); }
...
}
but the semantics of setX() are different than the standard setter convention for mutable bean objects, so somehow this doesn't feel right.
What's the best name for this method?
Should I call it withX() or newX() or something else?
edit: additional priority in my case: I have scripting clients (through JSR-223 and an object model I export) that can easily obtain a Foo object. It's cumbersome, however, to call constructors or create builders or whatever. So it's desirable for me to provide this method as a convenience for scripting clients.
addinstead ofsetX.)