Immutable objects the lazy way

Building properly immutable objects in Java can be annoying, especially if they’ve got a bunch of properties – too many to put into a readable constructor.1

You can implement the Builder pattern, but a lot of the time that just feels like overkill. But you don’t want to put in a bunch of setter methods, because that’s just asking for trouble. So what do you do?

Get with() it, of course. Replace all of your setXYZ() with methods with a withXYZ() method, that returns the object instance. Like so:

class Customer {
  private String name;

  public String getName() { return; }
  public Customer withName(String name) { = name; return this; }


  • This is very quick and easy to write. Heck, you can generate your setters and easily convert them. (The one-line form used above is easy to replace with a regex, for example)
  • Abuse is easy to detect – you just look for calls to withXYZ() that aren’t associated with a constructor.
  • Because you’re not using the standard Java Beans pattern, it doesn’t get picked up by any of the zillion tools that use the get/set pattern.
  • The caller can chain calls, resulting in a very literate coding style (not unlike ObjectiveC, actually).
  • It’s not a big deal to have a couple of parameters on a withXYZ() method, if the parameters are meant to go together – say, a withRocketAddress(double latitude, double longitude, double altitude)


  • Your object isn’t really immutable. People can still call the withXYZ method, and probably will if they don’t understand the convention. (They’ll probably also be cursing you and wondering why you don’t just call the methods setters). This isn’t that big a deal if you maintain code reviews – just invest in a cluebat2

  1. That’s four. Four parameters is too many to be in a constructor. Three is pushing it. 
  2. But only to wave around, not to use. Unless they really deserve it… 

Author: Robert Watkins

My name is Robert Watkins. I am a software developer and have been for over 20 years now. I currently work for people, but my opinions here are in no way endorsed by them (which is cool; their opinions aren’t endorsed by me either). My main professional interests are in Java development, using Agile methods, with a historical focus on building web based applications. I’m also a Mac-fan and love my iPhone, which I’m currently learning how to code for. I live and work in Brisbane, Australia, but I grew up in the Northern Territory, and still find Brisbane too cold (after 22 years here). I’m married, with two children and one cat. My politics are socialist in tendency, my religious affiliation is atheist (aka “none of the above”), my attitude is condescending and my moral standing is lying down.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: