Reduce boilerplate getters/setters in java
getters setters in java pojo classes always looks cluttered to me.I was searching for a way to to avoid writing those lazy get set blocks.
Project Lombok is a very good solution for that.It is an open source library that helps you reduce your boilerplate code.Such as using lombok you can avoid having to implement
- Getters and Setters
- Default ,No argument or All argument constructors
- toString() method
- equals() and hashcode () method
SetUp :
the first thing you have to do is to import lombok to your application.I have been using maven as build tool.So i added maven dependency for lombok.you can use the latest version for best benifits that is 1.18.12.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
Now you have to prepare your IDEA for lombok use.If you are using Intellij IDEA then its preety easy and straightforward.Go to Preference>plugin.Now search for lombok.now install it and restart your IDEA.thats cool isnot it?It also supports Eclipse,Netbeans and other IDEAS.

Now lets see how our pojo classes looks after using Lombok
/**
* User : Tanvir Ahmed
* Date: 9/15/2020.
*/
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Client {
private String name;
private String country;
private String initials;
private String prefix;
}
but previously we have to write it in this manner
/**
* User : Tanvir Ahmed
* Date: 9/15/2020.
*/
public class Client {
private String name;
private String country;
private String initials;
private String prefix;
public Client(String name, String country) {
this.name = name;
this.country = country;
}
public String getName() {
return name;
}
public String getCountry() {
return country;
}
public String getInitials() {
return initials;
}
public String getPrefix() {
return prefix;
}
public void setName(String name) {
this.name = name;
}
public void setCountry(String country) {
this.country = country;
}
public void setInitials(String initials) {
this.initials = initials;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
So in Lombok you just annotate your properties/fields with @Getter and @Setter and thats all to reduce lines of getters and setters from your code base.
For boolean data types the access of setter method is same as normal access rule.Just call the set method followed by the field name.but for getter is has a bit change .you have to call it with is.Say you a boolean filed private boolean isSick. so the getter will be isSick() and setter willbe setIsSick();
Constructors :
using lombok you can use annotation like
@NoArgsConstructor -Implement a public constructor with no argument.@AllArgsConstructor -implement a public constructor with all the properties of the class as argument@RequiredArgsConstructor- Implement a public constructor with all final properties and those marked with @Nonnull annotation only
Also you can fix the access modifier and default is public
@AllArgsConstructor(access =AccessLevel.PROTECTED)
Also you can implement equals and hashcode method using lombok with @EqualsAndHashCode annotation.
@EqualsAndHashCode
public class Client extends Person {
}
Also you can use @Builder annotation to use builder design pattern in ease.
/**
* User : Tanvir Ahmed
* Date: 9/15/2020.
*/
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder(builderClassName = "Builder", toBuilder = true)
public class Client {
private String name;
private String country;
private String initials;
private String prefix;
}
Client newClient= Client
.builder()
.name("Tanvir")
.build();
also instead of @ToString,@EqualsAndHashcode,@Getter,@Setter,@RequiredArgsConstructor you can use @Data annotation to achieve all this
//@AllArgsConstructor
//@NoArgsConstructor
//@Getter
//@Setter
//@Builder(builderClassName = "Builder", toBuilder = true)
@Data
public class Client {
private String name;
private String country;
private String initials;
private String prefix;
}