How to use @Builder (Lombok)

What is Lombok's @Builder annotation?

--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();

What is the initial value? ??

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.

How to specify the initial value 1: Inner class

--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";
    }
}

Initial value specification method 2: Use Default annotation

--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.

reference

https://reinhard.codes/2016/07/13/using-lomboks-builder-annotation-with-default-values/

Recommended Posts

How to use @Builder (Lombok)
How to use Lombok now
How to use Lombok in Spring
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use @Builder and @NoArgsConstructor together
How to use Map
[Java] How to use Map
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method
[Rails] How to use gem "devise"
How to use StringBurrer and Arrays.toString.
How to use arrays (personal memorandum)
How to use Java HttpClient (Get)