• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What are static factory methods

 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This blog has a good explanation.
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks A. Anand for the link,
looks like now I need to get my hands on design patterns

Thank you
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic