Quoting from "Effective
Java Programming Language" from Joshua Bloch
"Item 1: Consider providing static factory methods instead of
constructors
The normal way for a class to allow a client to obtain an instance is to provide a public
constructor. There is another, less widely known technique that should also be a part of every
programmer's toolkit. A class can provide a public static factory method, which is simply
a static method that returns an instance of the class. Here's a simple example from the class
Boolean (the wrapper class for the primitive type boolean). This static factory method, which
was added in the 1.4 release, translates a boolean primitive value into a Boolean object
reference:
public static Boolean valueOf(boolean b) {
return (b ? Boolean.TRUE : Boolean.FALSE);
}
A class can provide its clients with static factory methods instead of, or in addition to,
constructors. Providing a static factory method instead of a public constructor has both
advantages and disadvantages.
One advantage of static factory methods is that, unlike constructors, they have names.
If the parameters to a constructor do not, in and of themselves, describe the object being
returned, a static factory with a well-chosen name can make a class easier to use and the
resulting client code easier to read. For example, the constructor BigInteger(int, int,
Random), which returns a BigInteger that is probably prime, would have been better
expressed as a static factory method named BigInteger.probablePrime. (This static factory
method was eventually added in the 1.4 release.)
A class can have only a single constructor with a given signature. Programmers have been
known to get around this restriction by providing two constructors whose parameter lists
differ only in the order of their parameter types. This is a bad idea. The user of such an API
will never be able to remember which constructor is which and will end up calling the wrong
one by mistake. People reading code that uses these constructors will not know what the code
does without referring to the class documentation.
Because static factory methods have names, they do not share with constructors the restriction
that a class can have only one with a given signature. In cases where a class seems to require
multiple constructors with the same signature,
you should consider replacing one or more
constructors with static factory methods whose carefully chosen names highlight their
differences.
A second advantage of static factory methods is that, unlike constructors, they are not
required to create a new object each time they're invoked. This allows immutable classes
to use preconstructed instances or to cache instances as they're constructed and to
dispense these instances repeatedly so as to avoid creating unnecessary duplicate objects.
The Boolean.valueOf(boolean) method illustrates this technique: It never creates an object.
This technique can greatly improve performance if equivalent objects are requested
frequently, especially if these objects are expensive to create.
The ability of static factory methods to return the same object from repeated invocations can
also be used to maintain strict control over what instances exist at any given time. There are
two reasons to do this. First, it allows a class to guarantee that it is a singleton (Item 2).
Second, it allows an immutable class to ensure that no two equal instances exist:
a.equals(b) if and only if a==b. If a class makes this guarantee, then its clients can use
the == operator instead of the equals(Object) method, which may result in a substantial
performance improvement. The typesafe enum
pattern, described in Item 21, implements this
optimization, and the String.intern method implements it in a limited form.
A third advantage of static factory methods is that, unlike constructors, they can return
an object of any subtype of their return type. This gives you great flexibility in choosing
the class of the returned object."
[ April 01, 2008: Message edited by: Manuel Leiria ]