-
When and how should we use a constructor
Foo bar = new Foo(); -
And when and how should we use getInstance() (static factory methods)
Foo bar = Foo.getInstance();
What is the difference between these two? I have always used a constructor, but when should I use getInstance() instead?
Everybody seems to focus on singletons while I think that the question is actually about constructor vs static factory methods.
This is actually Item 1: Consider static factory methods instead of constructors of Effective Java by Joshua Bloch:
Advantages (quoting the book):
Disadvantages (still quoting the book):
classes without public or protected constructors cannot be subclassed.
readily distinguishable from other static methods.