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

Engineering
Demystifying Class Variables In Python

Understanding class variables in python

Read more...
Interview Experience: Backend Engineering Internship at Fyle

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

Read more...
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!

Read more...
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!

Read more...
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...

Read more...
The not so secret sauce of my work

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

Read more...
From Zero to Hero: The Policy Tests Journey!

The story of policy tests at Fyle

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

A blogpost that documents Shwetabh's journey at Fyle.

Read more...
Vikas Prasad @ Fyle

This document is a user guide to Vikas at work.

Read more...
Gokul K's README

This document is a user guide to Gokul at work.

Read more...

All Topics