Why Complex code with Getters and Setters in JAVA ? It’s time to move with Lombok

Why Complex code with Getters and Setters in JAVA ? It’s time to move with Lombok

Encapsulation is one of the four fundamental Object Oriented Concepts (OOP). The other three are inheritance, polymorphism, and abstraction.

Encapsulation in Java is a mechanism of wrapping the data and code acting on the data together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. And those methods are called as Getters and Setters, following code is some basic example for getters and setters.

public class EncapTest {
   private String name;
   private String id;
   private int age;

   public int getAge() {
      return age;
   }

   public String getName() {
      return name;
   }

   public String getId() {
      return id;
   }

   public void setAge( int newAge) {
      age = newAge;
   }

   public void setName(String newName) {
      name = newName;
   }

   public void setId( String newId) {
      id = newId;
   }
}

So today I’m going to talk you about Project Lombok which is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more. In the Lombok library there are few annotations. following are some of the annotations and it will easy our way of development and the time.

  • @getter for generate getters
  • @setter for generate setters
  • @EqualsAndHashCode for generate equals and Hash Codes

@Data annotation

@Data is a convenient shortcut annotation that bundles the features of @ToString, @EqualsAndHashCode, @Getter,@Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, setters for all non-final fields, and appropriate toString, equals and hashCode implementations that involve the fields of the class, and a constructor that initializes all final fields, as well as all non-final fields with no initializer that have been marked with @NonNull, in order to ensure the field is never null.

Then you don’t need to write getters, setters, equals by user self those methods will be generated by the Lombok library. Following is a example which they are shown in there documentation for @Data annotation After you see this you will enjoy coding :D

import lombok.AccessLevel;
import lombok.Setter;
import lombok.Data;
import lombok.ToString;

@Data public class DataExample {
  private final String name;
  @Setter(AccessLevel.PACKAGE) private int age;
  private double score;
  private String[] tags;

  @ToString(includeFieldNames=true)
  @Data(staticConstructor="of")
  public static class Exercise<T> {
    private final String name;
    private final T value;
  }
}

Happy Coding with Lombok :) …