Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7589435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:09:30+00:00 2026-05-30T20:09:30+00:00

Let’s say I have a class Foo in Java that has immutable data: class

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:09:32+00:00Added an answer on May 30, 2026 at 8:09 pm

    Original article: Immutable Setters: Naming Conventions (from Programming.Guide)


    withX(...)

    This is the de facto standard naming convention for immutable setters. This is for example the default name for setters generated by the Immutables framework. Here’s an example:

    Foo newFoo = foo.withX(1047);
    

    There is a @Value.Style option to change this pattern, but the option itself is called with="...", which emphasizes what the default convention is.

    Being the most widespread convention, it’s easy to find examples of this. Guava and the Java time package being two.

    Just x(...)

    Another approach is to not have a prefix at all. You see this in for example builders generated by the Immutables framework:

    Foo foo = ImmutableFoo.builder()
                          .x(1047)
                          .y("Hello World")
                          .build();
    

    If you use this approach directly on the immutable class (that is, no builder involved) you’d typically have it as an overload to the getter:

    Foo newFoo = foo.x(5);  // setter - one argument
    int x = newFoo.x();     // getter - no arguments
    

    This convention is used in for example the Java Spark framework.

    setX(...)

    Some APIs use the same naming convention as for setters in mutable classes. This has the obvious drawback that it can be surprising when you’re new to a code base. Working with BigInteger and writing…

    bigInt.setBit(2);
    

    …would for example be a mistake, since the returned object is discarded. With this naming pattern you have to get used to writing

    BigInteger newBigInt = bigInt.setBit(2);
    

    deriveX(...)

    To highlight the fact that the new value is derived from the existing object, you could use deriveX(...). The immutable Font class in the Java API follows this pattern. If you want to create a new font with, for example, a specific size you use

    Font newFont = font.deriveFont(newSize);
    

    The Font class has been around since the beginning of time. This convention is not very common as of today.

    Immutable object being an operand

    When the immutable object is itself an operand to the transformation it’s not really a setter in the traditional sense, and there’s no need to have a prefix for the method. For example…

    BigDecimal newBigDec = bigDec.multiply(BigDecimal.TEN);
    

    …has the same signature as a setter, but multiply is clearly a better method name than any other alternative.

    Same with String.substring, Path.resolve, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following entity: public class Store { public List<Product> Products
Let's imagine I have a Java class of the type: public class MyClass {
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have some text as follows: do this, do that, then this,
Let's say I have the string: hello world; some random text; foo; How could
Let's say that I have a set of relations that looks like this: relations

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.