I found an article with an interesting piece of code:
public class Employee {
private String firstName;
private String lastName;
//private default constructor
private Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public static Employee valueOf (String firstName, String lastName) {
return new Employee(firstName, lastName);
}
}
I am really curious in understanding the advantage of creating this kind of classes. I understand that here that an object of this class would be immutable, because there is no way of changing its variable values once initialized. I never did something like this before, and i dont really understand the advantage of it.
- Why is it a good practice?
- Could you name a situation where this approach can be used?
- What about constants or read only variables? Is not that very similar?
- In the article says, that this is not good for the performance of the application. But why?