Eliminate Boilerplate Java code with Lombok

I’ve been writing a lot of boilerplate Java code, lately — getters, setters, hashCode, equals and toString. Actually, I’d use Eclipse to generate these methods. There were some minor annoyances though, like when I needed to add a new member variable and then I had to regenerate code to include it.

It wasn’t pretty, but I had made my peace with it. Until I got to a class that had a lot of fields and I needed a builder to construct it. For those unfamiliar with the builder design pattern, here’s a quick primer. In a nutshell, I wanted to be able to write code like this:

Unfortunately, Eclipse doesn’t generate these builder classes and I sure as hell wasn’t going to write them by hand. So, I did what all developers do and headed to StackOverflow and stumbled upon this thread that mentioned Project Lombok. Project Lombok generates boilerplate code for you based on annotations. All I needed to do was to write the Java class like this:

The Data annotation generates getters, setters, hashCode, equals, toString and the Builder annotation generates the builder class. That’s it! I just followed the instructions on the Project Lombok website and was able to use it in my project in 10 mins.

Siva Narayanan

I am known to be "the CTO of one, the father of two, and the roasting baba of many."

More of our stories from

Demystifying Class Variables In Python

Understanding class variables in python

Interview Experience: Backend Engineering Internship at Fyle

Wanna know the secret to crack backend engineering interviews? Learn them here and intern at Fyle!

The curse of being a Senior Engineer, how to deal with timelines, frustrations, etc

Being a good developer is 50% skill and 50% emotional support; here's my secret to balancing both at the right amount!

How did I build cropping of receipts in the mobile app?

Follow Yash's journey of what it takes to reduce manual work for our customers when receipts come in all shapes and sizes!

How did we increase Data Extraction accuracy by a whopping ~50%?

Wanna know the secret of data extraction, the complex machine learning models we use, the experiments we did? Read on...

The not so secret sauce of my work

From chaos to clarity, follow Chethan's not so secret sauce to excelling at work!

From Zero to Hero: The Policy Tests Journey!

The story of policy tests at Fyle

How Fyle changed my life from a naive intern to a confident Engineering Lead

A blogpost that documents Shwetabh's journey at Fyle.

Vikas Prasad @ Fyle

This document is a user guide to Vikas at work.

Gokul K's README

This document is a user guide to Gokul at work.


All Topics