title: Java: pseudo-setter method for immutable classes
body: <p>Let's say I have a class Foo in Java that has immutable data:</p>
  
  <pre><code>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...
  }
  </code></pre>
  
  <p>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 <code>setX()</code>:</p>
  
  <pre><code>class Foo
  {
      ...
      Foo setX(int newX) { return new Foo(newX, this.otherstuff); }
      ...
  }
  </code></pre>
  
  <p>but the semantics of <code>setX()</code> are different than the standard setter convention for mutable bean objects, so somehow this doesn't feel right.</p>
  
  <p>What's the best name for this method?</p>
  
  <p>Should I call it <code>withX()</code> or <code>newX()</code> or something else?</p>
  
  <hr>
  
  <p>edit: additional priority in my case: I have scripting clients (through JSR-223 and an object model I export) that can easily obtain a <code>Foo</code> 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.</p>