This is a memo of the result of operation verification focusing on @ Getter
and @ Setter
of Lombok.
I won't write about how to install Lombok and how to use annotations other than @ Getter
and @ Setter
here, so please see what other people have put together.
Before getting into the main subject, I will briefly explain Lombok.
Lombok is a library that automatically generates methods such as getter
, setter
, and toString
by using specific annotations.
You can reduce the amount of description such as POJO objects at the development stage. There may be few people who write getters and setters by themselves these days, but ** spelling mistakes ** can be eliminated because they are automatically generated.
Let's use it now.
In this case, add @ Getter
and @Setter
to the fields for which you want to create getters and setters.
Product.java
import lombok.Getter;
import lombok.Setter;
public class Product {
@Getter
@Setter
private long id;
@Getter
private String name = "mouse";
}
If you check the outline, you can see that the getter and setter specified for the field have been created.
Check if it actually works.
public static void main(String[] args) {
Product product = new Product();
product.setId(1L);
System.out.println(product.getId());
System.out.println(product.getName());
}
console
1
mouse
You can see that each is working correctly like this.
Earlier we gave the fields @ Getter
and @Setter
, but in this case we give the class @ Getter
and @Setter
.
Product.java
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Product {
private long id;
private String name;
}
You can see that getters and setters have been created for all the fields in the class.
Operation verification is omitted.
In this case, annotate the class if you want to apply it to all fields, and annotate the field if you want to apply it to some fields.
Product.java
import lombok.Getter;
import lombok.Setter;
@Getter
public class Product {
@Setter
private long id;
private String name;
}
As you specified, the setter is created only in ʻid`.
Specify ʻAccessLevel.NONE if you want to create getters and setters for almost every field in your class, but not for some fields. ʻAccessLevel
allows you to change the access modifiers of the getters and setters that are created. If nothing is specified, it will be public
.
Product.java
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Product {
@Getter(AccessLevel.NONE)
private long id;
@Setter(AccessLevel.NONE)
private String name;
}
As you can see, the getter and setter with ʻAccessLevel.NONE` specified have not been created.
During development, you will come across a case where you want to change the behavior of getters and setters only for a part of the class.
In that case, implement getters and setters for the fields whose behavior you want to change.
This time, let's implement only the getter
of the name
field by ourselves.
Product.java
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Product {
private long id;
private String name;
public String getName() {
return "Product name: " + name;
}
}
From the outline, the order has changed, but you can see that there is a getter
.
Let's move it in the same way as before.
public static void main(String[] args) {
Product product = new Product();
product.setId(1L);
product.setName("keyboard");
System.out.println(product.getId());
System.out.println(product.getName());
}
console
1
Product name:keyboard
You can see that the getter implemented by yourself is called like this.