I've been reading in various places that it's often advisable to use static factory methods over public constructors.
One of the advantages is that unlike with constructors, a static factory method does not create a new object each time it is invoked. However, as I read on this site, the factory method for the class
class Employee {
private int _type;
static final int ENGINEER = 0;
static final int SALESMAN = 1;
static final int MANAGER = 2;
Employee (int type) {
_type = type;
}
}
is defined as :
static Employee create(int type) {
return new Employee(type);
}
Thus to create a new instance, we do
Employee eng = Employee.create(Employee.ENGINEER);
What I don't understand is, isn't the factory method invoking the public constructor? So isn't it creating a new instance all over again?
A second aspect of using static factory methods that I don't quite understand is why can classes without public / protected constructors not be subclassed?