--By adding @Builder to Java Class, Builder method is generated. --You don't have to prepare a long argument constructor.
import lombok.Builder;
//JavaClass on the called side
@Builder
public class Employee {
private String name;
private int syainId;
private int age;
private String position;
private String status;
}
//Caller of Builder
Employee employee = Employee.builder()
.name("Kohei Sato")
.syainId(101)
.age(32)
.position("developper")
.build();
In the above example, status is not specified. → status will be null. Since it is a String, it is null, but it seems that int is 0 and boolean is false.
--The default value can be specified in the class of ClassName + Builder
as shown below.
@Builder
public class Employee {
private String name;
private int syainId;
private int age;
private String position;
private String status;
public static class EmployeeBuilder {
private String status = "Active";
}
}
--Initialization allows you to specify the initial value when declaring a variable.
@Builder
public class Employee {
@Builder.Default private String name = "No Name";
@Builder.Default private int syainId = 0;
@Builder.Default private int age = 30;
@Builder.Default private String position = "Normal";
@Builder.Default private String status = "Active";
}
This one is easier to see personally.
https://reinhard.codes/2016/07/13/using-lomboks-builder-annotation-with-default-values/
Recommended Posts