I was reading about weather or not it's good practice to call a setter from the constructor on this SO page. I'm aware that you shouldn't call non-final methods from your constructor. One of the answers suggests:
Constructors should be cautious about leaking this before the instance is fully initialized.
I don't fully understand what the above quote means. Suppose I have the following class:
public final class Employee
{
private String empID;
private String empFirstName;
private String empLastName;
public Employee(String ID, String FirstName, String LastName)
{
//Call to other setters left out.
this.setEmployeeLastName(LastName);
}
//empID, empFirstName setter left out.
public void setEmployeeLastName(String lastname)
{
this.empLastName = lastname;
}
}
- Just as an example, how would I leak the this reference before the instance is fully created? Does it mean passing the object as an argument before it has been created?