Basic Guide To Lombok Java library For Java Developers

Posted By : Ankur Bansala | 25-Sep-2018

Lombok is a Java library intended to rearrange the advancement of Java code writing.

 

This blog manages writing classes that need getters/setters, supersede meets, hashCode, toString or potentially offer a copy constructor.

 

Lombok does this by means of comments that can be added to the Java class for which basic strategies are wanted.

 

The vast majority of the explanations are self-defined in their names:

 

@Getter, @Setter, @EqualsAndHashCode, @ToString, and @NoArgsConstructor are precedents. In this blog, I will show by going to apply basic Lombok feature to add these ordinarily composed strategies to a Java class.

 
 

Most of the projects are maven based, so I just typically drop their dependency in the provided scope:

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

 

Feature of Lombok Java library:

  1. No need to write getters and setters.

  Without Lombok

 
      
public class Animal {

    private String name;
    private String gender;
    private String species;

    public String getName(){
        return this.name;
    }

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

    public String getGender(){
        return this.gender;
    }

    public void setGender(String gender){
        this.gender = gender;
    }

    public String getSpecies(){
        return this.species;
    }

    public void setSpecies(String species){
        this.species = species;
    }
}

 

 

With Lombok

 
 
 
public class Animal {

    @Getter 
    @Setter 
    private String name;
    
    @Getter 
    @Setter 
    private String gender;
    
    @Getter 
    @Setter 
    private String species;
}

 

We can make Life just a little easier.

 

List of other feature in the Lombok API.

 

No need to override toString @ToString annotation over class level will override our toString method and print out the classes fields in Lombok.

 

No need to override equals and hashCode methods.  @EqualsAndHashCode annotation over class will generate equals and hashCode methods at compile time.

 

Generates constructors based on class annotations. To create a no argument constructor use @NoArgsConstructor annotation.

 

To create the constructor that takes one argument per non final/ non-null fields to use @RequiredArgsConstructor  annotation.

 

To create constructor takes in one argument for every field use

 

@AllArgsConstructor annotation. @Data shortcut  is used for @ RequiredArgsConstructor,@ToString and

 

@Getter / @Setter (on all non final fields).

 

Above common features of Lombok are very useful.



 

Example With and Without Lombok

 

We have a  serializable class which needs a copy constructor will override hashCode, toString, overriding equals, provide getter/setters for private fields, and a copy constructor.


 

With Lombok

 

 

@RequiredArgsConstructor
@ToString
@EqualsAndHashCode
public class Animal {
    @Getter @Setter private String name;
    @Getter @Setter private String gender;
    @Getter @Setter private String species;
}

 

 

Without Lombok

 
 
 
public class Animal {

    private String name;
    private String gender;
    private String species;

    public Animal(String name, String gender, String species) {
        this.name = name;
        this.gender = gender;
        this.species = species;
    }

    public String getName(){
        return this.name;
    }

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

    public String getGender(){
        return this.gender;
    }

    public void setGender(String gender){
        this.gender = gender;
    }

    public String getSpecies(){
        return this.species;
    }

    public void setSpecies(String species){
        this.species = species;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Animal)) return false;

        Animal animal = (Animal) o;

        if (gender != null ? !gender.equals(animal.gender) : animal.gender != null) return false;
        if (name != null ? !name.equals(animal.name) : animal.name != null) return false;
        if (species != null ? !species.equals(animal.species) : animal.species != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (gender != null ? gender.hashCode() : 0);
        result = 31 * result + (species != null ? species.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this) //Using guava library objects toString
                .add("name", name)
                .add("gender", gender)
                .add("species", species)
                .toString();
    }
}

 

 

Conclusion

Its subject of argument that in java development that "IDE's can generate that thing as well as you can annotate the class though!".

But I will say you on that you are missing the point.

 

Java is not much popular because of the heavy code of having to own and write all long code. The code is much simpler then java in Ruby, Groovy, Perl, or any scripting language. Simple is better, So Lombok is better for Java.

 

Boost your productivity by adding Lombok to your java API.this will increase your efficiency to work with Lombok annotations.Hope you like it.

 

 

About Author

Author Image
Ankur Bansala

Ankur is an experienced Java back-end Developer and having capabilities to build web application.

Request for Proposal

Name is required

Comment is required

Sending message..