When and how should we use a constructor
Foo bar = new Foo();
And when and how should we use getInstance() (static factory
The uses for getInstance methods:
But most of the time your object will be a simple POJO and usage of public constructors is most practical and obvious solution.
U1: getInstance From Another Class
To return an instance of a different class:
public class FooFactory {
public static Foo getInstance() {
return new Foo();
}
}
NumberFormat.getInstance methods do this as they actually return instances of DecimalFormat.
U2: Singleton Problems
The singleton pattern restricts many of the benefits of object oriented programming. Singletons generally have private constructors, therefore you cannot extend them. As you will be accessing it via its getInstance method and not referencing any interface, you will not be able to swap it out for another implementation.