Creation of Objects: Constructors or Static Factory Methods

Solution 1:

  • Advantage 4: When using a constructor, you have

    Foo<Map<Key, Value>> foo = new Foo<Map<Key, Value>>();
    

    vs

    Foo<Map<Key, Value>> foo = Foo.createFoo(); // no need to repeat
    

    this advantage will be gone in Java 7, when the diamond syntax will be introduced

  • Disadvantage 2. You can't easily tell whether a given static method is used for constructor, or for something else.

As for how to choose - there is no single recipe for that. You can weigh all of the above advantages and disadvantages given a use-case, but most often it will just be a decision driven by experience.

Solution 2:

If you know the concrete type of the class you are trying to create, then calling the Constructor is fine.

Static Factory Methods are nice when you're not entirely sure how to construct the object you need.

The Factory Method still calls the Constructor on the concrete type, but the method handles the decision on what kind of concrete class to instantiate. The Factory Method then returns an Interface type (rather than a concrete type) so that the concrete type is hidden from the caller.