DEV Community

Discussion on: Make an Immutable Object - in Java

Collapse
 
monknomo profile image
Gunnar Gissel

That's a nice way of doing it, and I prefer creating the immutable class from the builder.

I've found getters useful on builders for doing the final validation before creating the real immutable object - how do you usually handle validation?

Collapse
 
frothandjava profile image
Scot McSweeney-Roberts

A mix of in the not-a-setter method and the build method before calling the constructor (and maybe occasionally in the constructor, but not by preference). Depends on exactly why it's not valid.