Introduction to Project Lombok in Java and How to get started?

What is the need of Lombok?
Java is a very popular language but it has few drawbacks. One of the most popular drawback is that we still need to write the boilerplate codes like getters, setters, toString method in Java whereas Kotlin and Scala, which are also JVM based doesn’t need so and hence, this is the reason of their increased popularity in the community. This is where Lombok comes in picture and overcomes this drawback of Java.

What is Lombok?
Project Lombok is a java library tool which is used to minimize/remove the boilerplate code and save the precious time of developers during development by just using some annotations. In addition to it, it also increases the readability of the source code and saves space. But you might be thinking that nowadays, everyone uses IDEs which provides an option for generating these boilerplate codes, then what is the use of Lombok. Whenever we use IDEs to generate these boilerplate codes, we just save ourselves from writing all these codes but it is actually present in our source code and increases the LOC (lines of code) and reduces maintainability and readability. On the other hand, Lombok adds all these boilerplate codes at the compile-time in the “.class” file and not in our source code.

Let us compare our source code with and without using Lombok.

  • Without Lombok: A java model class with four private fields and their getters, setters, no args constructor, parameterized construct and toString method.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    public class Employee {
      
        private Integer employeeId;
        private String name;
        private String company;
        private String emailId;
      
        public Employee()
        {
        }
      
        public Employee(Integer employeeId, String name,
                        String company, String emailId)
        {
            super();
            this.employeeId = employeeId;
            this.name = name;
            this.company = company;
            this.emailId = emailId;
        }
      
        public Integer getEmployeeId()
        {
            return employeeId;
        }
      
        public void setEmployeeId(Integer employeeId)
        {
            this.employeeId = employeeId;
        }
      
        public String getName()
        {
            return name;
        }
      
        public void setName(String name)
        {
            this.name = name;
        }
      
        public String getCompany()
        {
            return company;
        }
      
        public void setCompany(String company)
        {
            this.company = company;
        }
      
        public String getEmailId()
        {
            return emailId;
        }
      
        public void setEmailId(String emailId)
        {
            this.emailId = emailId;
        }
      
        @Override
        public String toString()
        {
            return "Employee ["
                + "employeeId=" + employeeId
                + ", name=" + name + ", "
                + " company=" + company
                + ", emailId=" + emailId
                + "]";
        }
    }

    chevron_right

    
    

  • With Lombok: A java model class with four private fields and their getters, setters, no args constructor, parameterized construct and toString method using Lombok annotations.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import Lombok.AllArgsConstructor;
    import Lombok.Getter;
    import Lombok.NoArgsConstructor;
    import Lombok.Setter;
    import Lombok.ToString;
      
    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public class Employee {
        private Integer employeeId;
        private String name;
        private String company;
        private String emailId;
    }

    chevron_right

    
    

The difference is clearly visible in the codes. The original source code contains around 60 lines of code which has been reduced by Lombok to around 20 lines of code only. It not only reduces the lines of code but also increases the readability and maintainability of source code.

How to configure project Lombok to eclipse?

  1. Download the Lombok jar file from this official link.
  2. Double click on the downloaded jar file to execute it. A GUI will appear to specificy the IDE in which you want to plugin it.
  3. Click on specify location and select the IDE.
  4. Click on install to complete the installation and then quit the installer.
  5. To verify the correct installation, go to the Eclipse IDE, select help and then about the eclipse. You will see project Lombok installed there.

Lombok annotations:
Lombok provides a set of annotations to make our coding life easier. Let us look at the few most frequently used annotations of Lombok.

  1. @Getter and @Setter: These annotations provides the getter and setter methods for a field. These annotations can be used at both the levels, field as well as class. @Getter annotation generates a getter method with access type as public which simply returns the field and with name getName() if the field name is “Name”. @Setter annotation generates a setter method with access type as public which returns void and takes a single parameter to assign the value to the field. The default setter will have the name setName() if the field name is “Name”.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import Lombok.Getter;
    import Lombok.Setter;
      
    @Getter
    @Setter
    public class Employee {
        private Integer employeeId;
        private String name;
        private String company;
        private String emailId;
    }

    chevron_right

    
    

  2. @NoArgsConstructor: This annotation is used to generate a constructor with no arguments. It has empty body and does nothing. It is generally used in combination with some other parameterized constructor in use. It is required when you want to generate an object of the class by passing no arguments in the constructor.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import Lombok.NoArgsConstructor;
      
    @NoArgsConstructor
    public class Employee {
        private Integer employeeId;
        private String name;
        private String company;
        private String emailId;
    }

    chevron_right

    
    

  3. @AllArgsConstructor: This annotation is used to generate a parameterized constructor which accepts a single parameter for each field and initializes them using it. It is required when you want to generate an object of the class by passing the initial values of the fields in the constructor.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import Lombok.AllArgsConstructor;
      
    @AllArgsConstructor
    public class Employee {
        private Integer employeeId;
        private String name;
        private String company;
        private String emailId;
    }

    chevron_right

    
    

  4. @ToString: This annotation is used to override the toString() method and generate a default implementation for it. The default implementation prints the class name and the fields in order, separated by commas. You can also skip some fields that you don’t want to print by annotating them with @ToString.Exclude.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import Lombok.ToString;
      
    @ToString
    public class Employee {
        private Integer employeeId;
        private String name;
        private String company;
        private String emailId;
    }

    chevron_right

    
    

  5. @EqualsAndHashCode: This annotation is used to override the equals() and hashCode() methods and provides a default impluentation for thu. The default impluentation uses all the non-static fields and we can modify it and can exclude some fileds using the annotation @EqualsAndHashCode.Exclude.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import Lombok.EqualsAndHashCode;
      
    @EqualsAndHashCode
    public class Employee {
        private Integer employeeId;
        private String name;
        private String company;
        private String emailId;
    }

    chevron_right

    
    

  6. @Data: This annotation is a shortcut annotation and bundles @ToString, @Getter, @Setter, @EqualsAndHashCode and @RequiredArgsConstructor annotations into a single annotation. This annotation provides all the normally used boilerplate code in the model classes of java like getters for all the fields, setter for all the non-final fields, a default impluentation for toString(), equals() and hashCode() using all the fields of the class and a constructor that initializes all the fields of the class.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import Lombok.Data;
      
    @Data
    public class Employee {
        private Integer employeeId;
        private String name;
        private String company;
        private String emailId;
    }

    chevron_right

    
    

References:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.