You Might Stop Using Lombok’s @Builder After Reading This

Emanuel Trandafir
Javarevisited
Published in
4 min readJul 26, 2022

--

Photo by Tobias Wilden on Unsplash

1. Overview

Lombok is a Java library that can generate known patterns of code for us, allowing us to reduce the boilerplate code. For instance, we can leverage Lombok to generate all the getters and setters for a class.

In this article, we’ll discuss Lombok’s @Builder annotation, which implements the builder design pattern for us. We’ll see its advantages and some of the drawbacks it can bring.

After that, we’ll use Lombok’s configuration to achieve the same functionality in a more straightforward manner.

2. @Builder and Lombok’s Magic

Let’s start by looking into a simple use-case where the @Builder annotation can come in handy.

For the code examples in this article, we’ll use the Student data model:

Because our class is annotated with @Builder, we can instance a Student using syntax associated with the builder design pattern:

--

--

Emanuel Trandafir
Javarevisited

Hi, I'm Emanuel Trandafir, a Java developer from Romania. I have a strong passion for clean code, software design, and unit testing.