Home Lombok Lombok @Builder Examples

Lombok @Builder Examples

Lombok @Builder annotation produces the code automatically using Builder pattern. We can apply this annotation at class level, constructor level and method level. Builder pattern is a commonly used creational design pattern in application development which solves the instance creation problems with Factory and Abstract Factory design patterns.

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. Lombok @Builder using at Class level

When we annotate a class with @Builder, Lombok produces complex builder APIs for your class.

LombokBuilderDemo1.java :

Lomboked Code
Verify DeLomboked Code
@Getter
@Builder
public class LombokBuilderDemo1 {
	
	private Long id;
	
	private String name;
	
}

Testing Lomboked LombokBuilderDemo1.java

public class LombokBuilderDemo1Test {

	public static void main(String[] args) {
		
		LombokBuilderDemo1 lbd1 = LombokBuilderDemo1.builder()
				.name("Peter")
				.id(Long.valueOf(1))
				.build();
		
		System.out.println("id => "+lbd1.getId());
		System.out.println("name => "+lbd1.getName());

	}

}

Output Results :

id => 1
name => Peter

public class LombokBuilderDemo1 {
	private Long id;
	private String name;

	LombokBuilderDemo1(final Long id, final String name) {
		this.id = id;
		this.name = name;
	}


	public static class LombokBuilderDemo1Builder {
		private Long id;
		private String name;

		LombokBuilderDemo1Builder() {
		}

		public LombokBuilderDemo1.LombokBuilderDemo1Builder id(final Long id) {
			this.id = id;
			return this;
		}

		public LombokBuilderDemo1.LombokBuilderDemo1Builder name(final String name) {
			this.name = name;
			return this;
		}

		public LombokBuilderDemo1 build() {
			return new LombokBuilderDemo1(this.id, this.name);
		}

		@Override
		public String toString() {
			return "LombokBuilderDemo1.LombokBuilderDemo1Builder(id=" + this.id + ", name=" + this.name + ")";
		}
	}

	public static LombokBuilderDemo1.LombokBuilderDemo1Builder builder() {
		return new LombokBuilderDemo1.LombokBuilderDemo1Builder();
	}

	// Getters omitted
}

In the above example LombokBuilderDemo1Builder is the builder class and which is used to create instance for your class. If we wants to change the name of builder class, have to declare annotation like @Builder(builderClassName = "Builder").

To change build() name use buildMethodName attribute like @Builder(buildMethodName = "create") . To Change builder() name use builderMethodName attribute like @Builder(builderMethodName = "user")

NOTE: Like @AllArgsConstructor for @Builder also Lombok not generate constructor arguments for initialized final variables and static variables.

2. Lombok @Builder using at Constructor level

If not all the fields are required to instantiate your class, if only few of them required then declare @Builder annotation at constructor level which is having required arguments.

LombokBuilderDemo2.java :

Lomboked Code
Verify DeLomboked Code
@Getter
public class LombokBuilderDemo2 {

	private Long id;

	private String name;
	
	private boolean status;
	
	private int role;
	
	@Builder()
	public LombokBuilderDemo2(Long id, int role) {
		this.id = id;
		this.role = role;
	}
}

Testing Lomboked LombokBuilderDemo2.java

public class LombokBuilderDemo2Test {

	public static void main(String[] args) {

		LombokBuilderDemo2 lbd2 = LombokBuilderDemo2.builder()
				.id(Long.valueOf(1))
				.role(3)
				.build();

		System.out.println("id => " + lbd2.getId());
		System.out.println("role => " + lbd2.getRole());

	}

}

Output Results :

id => 1
role => 3
public class LombokBuilderDemo2 {
	private Long id;
	private String name;
	private boolean status;
	private int role;

	public LombokBuilderDemo2(Long id, int role) {
		this.id = id;
		this.role = role;
	}


	public static class LombokBuilderDemo2Builder {
		private Long id;
		private int role;

		LombokBuilderDemo2Builder() {
		}

		public LombokBuilderDemo2.LombokBuilderDemo2Builder id(final Long id) {
			this.id = id;
			return this;
		}

		public LombokBuilderDemo2.LombokBuilderDemo2Builder role(final int role) {
			this.role = role;
			return this;
		}

		public LombokBuilderDemo2 build() {
			return new LombokBuilderDemo2(this.id, this.role);
		}

		@Override
		public String toString() {
			return "LombokBuilderDemo2.LombokBuilderDemo2Builder(id=" + this.id + ", role=" + this.role + ")";
		}
	}

	public static LombokBuilderDemo2.LombokBuilderDemo2Builder builder() {
		return new LombokBuilderDemo2.LombokBuilderDemo2Builder();
	}

	// Getters omitted
}

3. Lombok @Builder using at Method level

If your class already have a method to create instance, you can make it as builder by declaring @Builder on that method.

LombokBuilderDemo3.java :

Lomboked Code
Verify DeLomboked Code
@Getter
public class LombokBuilderDemo3 {

	private Long id;

	private String name;
	
	private boolean status;
	
	private int role;
	
	public LombokBuilderDemo3(Long id, String name, int role, boolean status) {
		this.id = id;
		this.name = name;
	}
	
	@Builder(builderMethodName = "builder")
	public static LombokBuilderDemo3 createInstance(Long id, String name) {
		// create instance with default role and status
		return new LombokBuilderDemo3(id, name, 1, false);
	}
}

Testing Lomboked LombokBuilderDemo3.java

public class LombokBuilderDemo3Test {

	public static void main(String[] args) {
		
		LombokBuilderDemo3 lbd3 = LombokBuilderDemo3.builder()
				.id(Long.valueOf(1))
				.name("Peter")
				.build();
		
		System.out.println("id => "+lbd3.getId());
		System.out.println("name => "+lbd3.getName());
		System.out.println("role => "+lbd3.getRole());
		System.out.println("status => "+lbd3.isStatus());

	}

}

Output Results :

id => 1
name => Peter
role => 0
status => false

public class LombokBuilderDemo3 {
	private Long id;
	private String name;
	private boolean status;
	private int role;

	public LombokBuilderDemo3(Long id, String name, int role, boolean status) {
		this.id = id;
		this.name = name;
	}

	public static LombokBuilderDemo3 createInstance(Long id, String name) {
		// create instance with default role and status
		return new LombokBuilderDemo3(id, name, 1, false);
	}


	public static class MyBuilder {
		private Long id;
		private String name;

		MyBuilder() {
		}

		public LombokBuilderDemo3.MyBuilder id(final Long id) {
			this.id = id;
			return this;
		}

		public LombokBuilderDemo3.MyBuilder name(final String name) {
			this.name = name;
			return this;
		}

		public LombokBuilderDemo3 build() {
			return LombokBuilderDemo3.createInstance(this.id, this.name);
		}

		@Override
		public String toString() {
			return "LombokBuilderDemo3.MyBuilder(id=" + this.id + ", name=" + this.name + ")";
		}
	}

	public static LombokBuilderDemo3.MyBuilder builder() {
		return new LombokBuilderDemo3.MyBuilder();
	}

	// Getters omitted
}

4. Conclusion

In this guide we have covered Lombok Builder annotation with several examples using different options. 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 :

  1. Lombok Builder default values Examples
  2. Lombok @Singular
  3. Lombok @Getter @Setter and lazy getters
  4. Lombok @EqualsAndHashCode
  5. Lombok @AllArgsConstructor
  6. Lombok @NoArgsConstructor
  7. Lombok @RequiredArgsConstructor
  8. Lombok @Slf4j
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

2 COMMENTS

  1. Class resource Satish!! Any chance you could convert the section on the right to a Nav section and make it hideable so you can see the whole class without scrolling?

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories