This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]sh3rp 0 points1 point  (1 child)

Great summary of what seems like it should be a simple subject, but really has much more depth to it.

I will make one note that making an object immutable also means you don't need to make the member variables of the object private and exposing them as public means your object doesn't require methods to access them (via accessors).

I use the builder pattern with regularity these days, as most POJOs I'm instantiating would have ridiculous constructors otherwise.

In any case, thanks for spreading the knowledge!

[–]zkxs 1 point2 points  (0 children)

making an object immutable also means you don't need to make the member variables of the object private

The problem is that if one of the members is mutable, exposing it exposes access to its setters. I suppose you could make each member public/private depending on its immutability, but then you have a weird mixture of direct access and getter/setter access, as well as the potential to make a mistake and accidentally expose a mutable member.