📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Let's create a step-by-step example to demonstrate generating builder APIs for the User class.
What is the @Builder Annotation?
Project Lombok Maven
- Create a simple maven project using the - How to Create a Simple Maven Project in Eclipse article.
- Add the below dependency in your maven project pom.xml file:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>latest-version</version>
<scope>provided</scope>
</dependency>
Adding the Lombok Plugin in IDE (Eclipse)
- Downloaded the jar from https://projectlombok.org/download or use the jar which is downloaded from your maven build.
- Execute command in terminal: java -jar lombok.jar
- This command will open the window as shown in the picture below, install and quit the installer and restart Eclipse.
With Project Lombok
package net.javaguides.lombok.builder;
import lombok.Builder;
import lombok.ToString;
@Builder
@ToString
public class User {
private long id;
private String firstName;
private String lastName;
private int age;
private String gender;
}
Testing
package net.javaguides.lombok.builder;
import net.javaguides.lombok.builder.User.UserBuilder;
public class LombokBuilderTest {
public static void main(String[] args) {
// create builder class and add parameters to it.
UserBuilder builder = new UserBuilder();
builder.id(100 L);
builder.firstName("Ramesh");
builder.lastName("Fadatare");
builder.age(28);
builder.gender("Male");
builder.build();
System.out.println();
System.out.println(builder.toString());
System.out.println();
}
}
Generated UserBuilder Class
Using @Builder annotation, when generating the class, a new Builder class will be added. With the above example, if you check the User class in folder /target/classes/net/javaguides/lombok, you will see the content below:package net.javaguides.lombok.builder;
public class User
{
private long id;
private String firstName;
private String lastName;
private int age;
private String gender;
public static class UserBuilder
{
private long id;
private String firstName;
private String lastName;
private int age;
private String gender;
public String toString()
{
return "User.UserBuilder(id=" + this.id + ", firstName=" + this.firstName + ", lastName=" + this.lastName + ", age=" + this.age + ", gender=" + this.gender + ")";
}
public User build()
{
return new User(this.id, this.firstName, this.lastName, this.age, this.gender);
}
public UserBuilder gender(String gender)
{
this.gender = gender;return this;
}
public UserBuilder age(int age)
{
this.age = age;return this;
}
public UserBuilder lastName(String lastName)
{
this.lastName = lastName;return this;
}
public UserBuilder firstName(String firstName)
{
this.firstName = firstName;return this;
}
public UserBuilder id(long id)
{
this.id = id;return this;
}
}
User(long id, String firstName, String lastName, int age, String gender)
{
this.id = id;this.firstName = firstName;this.lastName = lastName;this.age = age;this.gender = gender;
}
public static UserBuilder builder()
{
return new UserBuilder();
}
public String toString()
{
return "User(id=" + this.id + ", firstName=" + this.firstName + ", lastName=" + this.lastName + ", age=" + this.age + ", gender=" + this.gender + ")";
}
}
qw
ReplyDelete