So I have seen a couple of examples on Builder Pattern. In all, The static class or Builder only has those memebers as final that can be intitialized by the constructor. Example -
public class User {
private final String firstName; // required
private final String lastName; // required
private final int age; // optional
private User(UserBuilder builder) {
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.age = builder.age;
this.phone = builder.phone;
this.address = builder.address;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public static class UserBuilder {
private final String firstName;
private final String lastName;
private int age;
public UserBuilder(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public UserBuilder age(int age) {
this.age = age;
return this;
}
}
}
Why is that only First and Last Name are final that can be intitialized by the constructor? And it cannot be set after that? My doubt is Can't I remove the final from First and Last and have a constructor which initializes all three members and have corresponding setters too in the class?