If you make the Builder a static inner class then you can give the class you're building a private constructor that takes an object of the Builder class. So you end up with
final Dog dog = ImmutableDog.builder().name("fido").weight(20).build();
Doing it this way enforces using the builder and I've found it helps with maintenance
You might have noticed that I don't bother with get/set prefixes on Builders - they're not Beans and I'm probably never going to read a value that I'm putting into the builder.
Saving fish by writing code! Applications developer in fisheries, specializing in webapps and moving 'enterprise-y' legacy systems to modern agile systems - Email or tweet me if you want to talk!
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If you make the Builder a static inner class then you can give the class you're building a private constructor that takes an object of the Builder class. So you end up with
final Dog dog = ImmutableDog.builder().name("fido").weight(20).build();
Doing it this way enforces using the builder and I've found it helps with maintenance
You might have noticed that I don't bother with get/set prefixes on Builders - they're not Beans and I'm probably never going to read a value that I'm putting into the builder.
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?
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.