Introduction to Lombok

What is Project Lombok?

Lombok is a popular Java library that helps to reduce boilerplate code in Java applications. It provides a set of annotations that can be used to generate getters, setters, constructors, and other repetitive code automatically in compile-time. The library is available under the MIT License and can be used in both commercial and non-commercial projects.

In this blog post, we will explore the basics of Lombok library and how it can be used to make Java development more efficient.

To appreciate the benefits of Lombok, first we have to understand how a typical Java class will look like when Lombok is not used. And then a comparison on using Lombok can help you understand how Lombok improves productivity.

public class Person {
    private String firstName;
    private String lastName;
    
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
   
}

In the above Java class, for each private field, we have created 2 public getter and setter methods. Assumer there are 15 such fields, and we will be having 30 such methods in total.

In the coming section, we will see how we can remove these boilerplate code from the class using Lombok.

How to Use Lombok?

To use Lombok in your Java project, you need to add the Lombok dependency to your project’s build file. For example, if you are using Maven, you can add the following dependency to your pom.xml file:

<dependency>    
   <groupId>org.projectlombok</groupId>    
   <artifactId>lombok</artifactId>    
   <version>1.18.28</version>    
   <scope>provided</scope>
</dependency>

Version 1.18.28 is the latest at the time of writing this post. For knowing the latest version, you can find in Maven Repository.

Once you have added the Lombok dependency, you can start using the Lombok annotations in your Java classes. Here are some examples of how to use Lombok annotations:

@Getter and @Setter

The @Getter and @Setter annotations can be used to generate getters and setters for your class properties. For example:

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Person {    
  private String firstName;    
  private String lastName;
}

Now you can see that all getter and setter methods are no longer needed, as they are automatically generated by Lombok at compile time. Your class is very clean and easy to maintain without these boilerplate code. There will be difference on how you will be using these methods.

@NoArgsConstructor and @AllArgsConstructor

The @NoArgsConstructor and @AllArgsConstructor annotations can be used to generate constructors automatically for your class. For example:

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
public class Person {    
  private String firstName;    
  private String lastName;
}

This code will generate the following constructors at compile-time:

public Person() {
  public Person(String firstName, String lastName) {    
    this.firstName = firstName;    
    this.lastName = lastName;
  }
}

@ToString

The @ToString annotation can be used to generate a toString() method for your class. For example:

import lombok.ToString;

@ToString
public class Person {    
  private String firstName;    
  private String lastName;
}

This code will generate the following toString() method:

public String toString() {    
  return "Person(firstName=" + firstName + ", lastName=" + lastName + ")";
}

Conclusion

Lombok is a powerful Java library that can help to reduce boilerplate code in your Java applications. By using Lombok annotations, you can generate getters, setters, constructors, and other repetitive code automatically, which can save you time and reduce the risk of errors. We are sure it can increase your development workflow and save lot of time.

Leave a Reply

Your email address will not be published. Required fields are marked *