Why should we use Lombok's @Builder annotation ?
Tags: java, codequality
Published: 2021-07-26 19:35:42

Have you also ever looked at the long list of getters and setters methods in an Object class, like this User class (for example)

![User class with lots of getters and setters](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/43tnb0iip3wxejle1k0p.png)

and got a gut feeling that all these boilerplates are useless. There must be a better way to achieve similar functionality ?

Well, we were correct!

#### What to expect out of this article ?
In this blog, We’ll be discussing what Lombok's @Builder annotation is and why we need it in the first place? Bit about how we can use it.

#### Why am I writing this ?
This is to set the context for next article. In which I'll be sharing with you a recent learning which we had in our project, of course, related to this Lombok's @Builder.

Before diving into this annotation, let’s review what Lombok is.

## What

Recently, I have started working in Java and that’s how I came to know about Legendary Lombok. For those people who haven’t encountered it yet (like me, until recently) - 

> [Lombok](https://www.projectlombok.org/) is a Java library. Easily integratable with our IDE, build tools. It provides getter, setter, constructors, few other default functions like equals, etc. We just have to use it’s annotations. It works out of the box.

@Builder annotation enforces [Builder Design Pattern](https://refactoring.guru/design-patterns/builder). It provides APIs to build objects of the annotated class. It also provides an option to convert existing objects into it’s builder, so that we can modify and rebuild it.

## Why

If we have to build object of the User class shown above, (without using Builder) we’ll be doing something like this:

![Creating a User class object using setters](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4gtaqu7h0pwxuhqcqult.png)

This is still okaish, we’re just setting each of the properties of the user class object. What's more annoying is that there is a long list of getter and setter methods in the User class. Which are nothing but boilerplates. So kinda frustrating.

## How
This is where @Builder annotation of the Lombok comes into picture. If we just annotate our User class with @Builder then all the getter setter boilerplates from our User class will be striped out. The Object creation would get simplified something like:

![Creating a User class object using Builder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdruujn3tc1t1vjro7dj.png)

You see, how intuitive it becomes ? It’s just like assigning properties value and building it.

So this is how we create User objects from a builder or raw values. But, what if we want to convert an existing object to it’s builder equivalent.

For that purpose, @Builder provides a configuration parameter toBuilder. This is how we configure and use it:

![Converting User object to it's Builder using toBuilder()](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mac66yordqj2i4no9bxb.png)

That's a quick intro about Lombok, @Builder and it's toBuilder.

Watch out for next article regarding our recent learning experience.

Just to ignite the curiosity in you, so we we're modifying an object exactly similar to one shown in above example. As a result, instead of updating same object it was behaving bit weirdly. That's how we uncovered an interesting part of this. Which we'll be covering in next article.

[Looking forward to your feedback](https://forms.gle/aRQB23pJN4kJtvoN9)