public class User {
private String name;
private int age;
private String email;
private User(Builder builder) {
this.name = builder.name;
this.age = builder.age;
this.email = builder.email;
}
// Getter methods
public static class Builder {
private String name;
private int age;
private String email;
public Builder() {
}
public Builder withName(String name) {
this.name = name;
return this;
}
public Builder withAge(int age) {
this.age = age;
return this;
}
public Builder withEmail(String email) {
this.email = email;
return this;
}
public User build() {
return new User(this);
}
}
}
// Usage
User user = new User.Builder()
.withName("John")
.withAge(30)
.withEmail("john@example.com")
.build();