Constructors have a tendency to grow in size as code matures. Deciding to use a normal constructor or using a builder mechanism is dependent on the number of attributes you need to set when instantiating objects. If you have only 3-4 parameters to the constructor there is no need to go for a builder. The default setting in checkstyle is a maximum of 7 parameters before it complains.

Turning on checkstyle on the code below gives the following message: ProjectMain.java:21:12: More than 7 parameters (found 10). [ParameterNumber]. A builder is a good way to improve the code and avoid difficult to understand constructors.

Code from this blog post are available at https://github.com/refreshjavaio/onebuilder. The example above is available on the branch SNAP-001. Checkstyle is set up with default Sun code style settings and modified to make the examples simple. Running maven clean install gives the following error message:

Using a builder can solve the issue of having to many parameters in a constructor when all the fields are not mandatory. It also takes away the problems with having multiple constructors for different purposes. Note that a builder still should have a constructor with the mandatory fields that always needs to be set. The constructor should also provide default values for other fields that are final in the class being built. Also worth mentioning is that there is no rule against having multiple parameters in a builder method. An example of this is provided in the finance method in the example below.

Suggested code below is available on GitHub on branch SNAP-002