Getter/Setter methods
A POJO(Plain Old Java Object) class contains fields or instance variables and corresponding methods to get the value of these fields and set their values.
These methods are called getter and setter methods and follow a naming convention which is prefixing get and set before the field name, such as getName()
and setName()
for name
field.
POJO classes are generally used as Data Transfer Objects(DTOs) to carry values from backend server to front end UI and vice versa or to transfer data within different layers of an application.
This is where getter and setter methods are used.
These methods are regarded as boiler plate code. Most IDEs provide support to automatically generate these methods.
But, the presence of these methods increases the length of classes and promotes code redundancy.
public class User { private String firstName; private String lastName; private String address; private int age; private String gender; public String getFirstName() { return firstName; } public void setFirstName(String fname) { firstName = fname; } public String getLastName() { return lastName; } public void setLastName(String lname) { lastName = lname; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(String age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }
You can see the amount of boiler plate code created with getter and setter methods.
Project lombok attempts to remove these methods from a class definition and replaces them with a simple annotation.
@Getter/@Setter annotations
Annotate a field with these annotations and lombok will generate these methods automatically. You do not need to write or generate these methods.
These annotations can also be applied at class level, which means that it will generate getter and setter methods for all the fields.
So, with these lombok annotations, above class will be modified to
import lombok.Getter; import lombok.Setter; @Getter @Setter public class User { private String firstName; private String lastName; private String address; private int age; private String gender; }
You can see how clean the class looks now. Lombok automatically generates getter and setter methods for each field.
Below is an example of using a POJO that is marked with lombok annotations.
public class LombokGetterSetterTest { public static void main(String[] args) { User user = new User(); user.setFirstName("A"); user.setLastName("B"); user.setAge(22); System.out.println(user.getFirstName()); System.out.println(user.getLastName()); } }
Notice that we did not write any getter/setter methods in our class, they are generated automatically.
@Getter/@Setter on fields
You can choose to generate getter and setter methods for some fields and create them yourself for other fields, by applying @Getter
/@Setter
on individual fields rather than at class level. Example,
public class User { private String name; private String address; @Getter @Setter private int age; @Getter private String gender; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
In this case, lombok will generate getter methods for age
and gender
fields and setter method for age
field.
Disabling method generation
Suppose you have applied @Getter
and @Setter
over class. This means that lombok will generate these methods for all the fields of the class.
But what if, you do not want to generate setter method for one of the fields, such as, for preventing it from being modified.
This can be done using AcessLevel.NONE
as shown below.
@Getter @Setter public class User { private String firstName; private String lastName; private String address; private int age; // override default behavior @Setter(AccessLevel.NONE) private String gender; }
You can also control the visibility of the generated getter and setter methods using AccessLevel
alongwith its PUBLIC
, PRIVATE
, PROTECTED
and PACKAGE
values, which correspond to java access specifiers.
Default scope is public.
Avoiding get and set
Getter/Setter methods are generated as field names prefixed with get and set by default. Lombok provides flexibility to avoid get/set and generate these methods with only the field names.
For this add @Accessors
annotation with fluent attribute set to true
as shown below.
import lombok.Getter; import lombok.Setter; import lombok.experimental.Accessors; @Getter @Setter @Accessors(fluent = true) public class User { private String firstName; private String lastName; private String address; private int age; private String gender; }
Now, the getter/setter methods will be generated with same names as the field names such as age()
, gender()
etc.
Getter method will not accept any argument while setter will accept an argument of the same type as the field.
Note that you are still required to add @Getter
and @Setter
annotations.
This is the same as records introduced in java 16.
Hope the article was useful.