wood burning stoves
The moose likes Beginning Java and the fly likes What are static factory methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What are static factory methods" Watch "What are static factory methods" New topic

What are static factory methods

Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2851

Hi all,
I know this question is too novice, but as this forum says no question is too small to ask.
Anyways, although I cleared SCJP 5, the only thing I came to know about is that when we need a Date instance, we use the static factory method Calendar.getDateInstance(). As static factory methods were not on the exam I just read on. But then I could not find more material on the topic.
Could anyone please explain and also why such method exist in place of a constructor.
Thanks in advance.

|Asking Good Questions|
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

This blog has a good explanation.
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Quoting from "Effective Java Programming Language" from Joshua Bloch

"Item 1: Consider providing static factory methods instead of
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
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
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 ]

Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2851

Thank you for the replies, but now reading the above mentioned reply and the blog, one more question comes to my mind that what is a singleton.The only thing I know about a singleton is that it has only a single instance.
Apart from this what is the significance of it and how to write a singleton?

Thank you in advance
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Singleton actually comes in picture as a design pattern. In this pattern only one single instance is available. As an example you can have your database connection factory class as singleton and this will help to have only one single instance of database.

You can read this article to know more how to implement singleton pattern.

Hope that helps.
Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2851

Thanks A. Anand for the link,
looks like now I need to get my hands on design patterns

Thank you
I agree. Here's the link: http://aspose.com/file-tools
subject: What are static factory methods
jQuery in Action, 3rd edition