In this article we will see couple of examples how we can set default values to fields when we are using Builder pattern with Lombok.
Make sure you already installed Lombok setup for your IDE. To Setup in Eclipse or in Spring Tool Suite refer to our Lombok Maven example setup with Eclipse.
1. Builder Example without @Builder.Default
If you set any default values to the fields, they never considered during build the object.
@ToString @Builder(builderClassName = "Builder") public class LombokBuilderDefaultValueDemo1 { private Long id; private String name = "anonymous"; private boolean active; private int role; private double salary; private int defaultRole = 3; }
Testing LombokBuilderDefaultValueDemo1 :
public class LombokBuilderDefaultValueTest1 { public static void main(String[] args) { LombokBuilderDefaultValueDemo1 lbdvd1 = LombokBuilderDefaultValueDemo1.builder() .build(); System.out.println(lbdvd1); } }
Output Results :
LombokBuilderDefaultValueDemo1(id=null, name=null, active=false, role=0, salary=0.0, defaultRole=0)
2. Builder Example with @Builder.Default
If you wants to set default values for the fields annotate them with @Builder.Default.
import lombok.Builder; import lombok.Builder.Default; import lombok.ToString; @ToString @Builder(builderClassName = "Builder") public class LombokBuilderDefaultValueDemo1 { private Long id; @Default private String name = "anonymous"; private boolean active; private int role; private double salary; @Default private int defaultRole = 3; }
No run again the LombokBuilderDefaultValueTest1 , you will get following results.
LombokBuilderDefaultValueDemo1(id=null, name=anonymous, active=false, role=0, salary=0.0, defaultRole=3)
3. Using toBuilder and deafult constructor (no arguments)
If you instantiate using default / no argument constructor, it constructs the values with provided defaults. But in this case you will loose the builder pattern capabilities. To get it work we can use @Builder with toBuilder = true and @NoArgsConstructor. Let’s see following example.
@ToString @Builder(toBuilder = true, builderClassName = "Builder") @NoArgsConstructor @AllArgsConstructor public class LombokBuilderDefaultValueDemo2 { private Long id; private String name = "anonymous"; private boolean active; private int role; private double salary; private int defaultRole = 3; }
Testing LombokBuilderDefaultValueDemo2 :
public class LombokBuilderDefaultValueTest2 { public static void main(String[] args) { LombokBuilderDefaultValueDemo2 lbdvd2 = new LombokBuilderDefaultValueDemo2(); System.out.println(lbdvd2); LombokBuilderDefaultValueDemo2 lbdvd3 = new LombokBuilderDefaultValueDemo2().toBuilder() .build(); System.out.println(lbdvd3); // toBuilder() LombokBuilderDefaultValueDemo2 lbdvd4 = new LombokBuilderDefaultValueDemo2().toBuilder() .id(Long.valueOf(2)) .active(true) .role(5) .build(); System.out.println("toBuilder => "+lbdvd4); // builder() LombokBuilderDefaultValueDemo2 lbdvd5 = LombokBuilderDefaultValueDemo2.builder() .id(Long.valueOf(2)) .active(true) .role(5) .build(); System.out.println("builder => "+lbdvd5); } }
Output Results :
LombokBuilderDefaultValueDemo2(id=null, name=anonymous, active=false, role=0, salary=0.0, defaultRole=3) LombokBuilderDefaultValueDemo2(id=null, name=anonymous, active=false, role=0, salary=0.0, defaultRole=3) toBuilder => LombokBuilderDefaultValueDemo2(id=2, name=anonymous, active=true, role=5, salary=0.0, defaultRole=3) builder => LombokBuilderDefaultValueDemo2(id=2, name=null, active=true, role=5, salary=0.0, defaultRole=0)
4. Conclusion
In this article we have seen couple of examples how we can set default values to fields when we are using Builder pattern with Lombok.
You can refer Delombok Maven example to see how looks like lombok generated code for your Lomboked classes.
You can checkout source code at github.
You might be interested in our other following Lombok Tutorials :
- Lombok Data annotation
- Lombok @Value
- Lombok @Builder
- Lombok @Singular Examples with Builders
- Lombok @NonNull
- Lombok Spring Boot example