HomeTechnologyLombok in Java - Knoldus Blogs

Lombok in Java – Knoldus Blogs

Reading Time: 3 minutes

1651618300 582 Lombok in Java Knoldus Blogs

Lombok acts as an annotation processor that provides code to your classes at compile time.

Project Lombok is a java library tool that’s used to minimize or remove the boilerplate code.

Also, save the precious time of the developers during development by just using some annotations.

Now the question arises what exactly the Boilerplate code is?

The Boilerplate code is sections of code that are repeated in multiple places with tiny to no variation.


In the Getter and Setter in the JavaBean model class,

we can reduce such boilerplate code by using the @Getter / @Setter annotation of Lombok.

Project Lombok is an excellent tool to make coding in Java easy.

And it is a fine answer to devs who say that Java codes are extremely large when compared to Python. Lombok helps the developers to focus on the implementation by not worrying about writing the boilerplate codes and keeping the final code shorter.

By using Lombok’s simple annotations, you just need to focus on the business logic in your class, and Lombok will take care of details desired by the Java compiler and JVM. 

Comparison of code with and without Lombok

Without Lombok:

public class Student {

	private Integer studentId;
	private String name;
	private String course;
	private String emailId;
        private string address;

	public Student() {}

	public Student(Integer studentId, String name,
					String course, String emailId, String     address)
		this.studentId = studentId;
		this.name = name;
		this.course = course;
		this.emailId = emailId;
                this.address = address;

	public Integer getStudentId() { return studentId; }

	public void setStudentId(Integer studentId)
		this.studentId = studentId;

	public String getName() { return name; }

	public void setName(String name) { this.name = name; }

	public String getCourse() { return course; }

	public void setCourse(String course)
		this.course = course;

	public String getEmailId() { return emailId; }

	public void setEmailId(String emailId)
		this.emailId = emailId;
        public String getAddress() { return address; }

	public void setAddress(String address)
		this.address = address;

	@Override public String toString()
		return "Student ["
			+ "studentId=" + studentId + ", name=" + name
			+ ", "
			+ " course=" + course + ", emailId=" + emailId
			+ ", address=" + address+ "]";

With Lombok:

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

public class Student {
	private @Getter @Setter Integer studentId;
	private @Getter @Setter String name;
	private @Getter @Setter String course;
	private @Getter @Setter String emailId;
        private @Getter @Setter String address;


The difference is clearly visible in the above code.

Source code without Lombok contains many lines of code. On Another hand code with Lombok contains only a few lines of code.

It not only reduces the lines of code but also increases the readability and maintainability of source code.



Some Lombok Annotations

Now, let’s see some common examples of different Lombok Annotations:

 @NoArgsConstructor,@AllArgsConstructor,@Data, and @NonNull, standard @Getter, @Setter, @ToString, etc.

@AllArgsConstructor generates a constructor requiring an argument for every field in the annotated class.

@NoArgsConstructor generates a constructor with no parameter.

@Data helps to reduce the amount of code(annotations to represent each function) used.

 @NonNull generate a null check on a setter field.

@ToString will cause Lombok to generate an implementation of the toString() method.


Lombok can make your life as a programmer a lot simpler, reduce the size of your code, and improves readability.

References: https://en.everybodywiki.com/Lombok_(Java_Library) https://www.youtube.com/watch?v=745W-dng3wk

1651618300 902 Lombok in Java Knoldus Blogs


Most Popular