This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Instance factory methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance factory methods" Watch "Instance factory methods" New topic
Author

Instance factory methods

Gina vernon
Ranch Hand

Joined: Jan 16, 2009
Posts: 108
Hi,
Does anyone have an example or scenario where instance factory methods are applicable? What are their advantages over a regular constructors?
Thanks.
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
Check out this JavaWorld article
Gina vernon
Ranch Hand

Joined: Jan 16, 2009
Posts: 108
Larry Frissell wrote:Check out this JavaWorld article

Larry,
Looks like the article is talking only about static factory methods. I am assuming static and instance factory methods are 2 seperate concepts. Right/wrong?
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
I am afraid that I do not know if those are two different concepts.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
Most factory methods are static because they have to dispense objects before an object of the class is created.

Apart from the error mentioned in the comments, the article Larry Frissell quoted looks quite good. You can find more, but I would recommend trying Joshua Bloch's book for more details.
Gina vernon
Ranch Hand

Joined: Jan 16, 2009
Posts: 108
Campbell Ritchie wrote:Most factory methods are static because they have to dispense objects before an object of the class is created.

Apart from the error mentioned in the comments, the article Larry Frissell quoted looks quite good. You can find more, but I would recommend trying Joshua Bloch's book for more details.


Campbell,
I am looking for information about instance factory methods. Assuming they are not the same as static factory methods.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If you look in the javax.xml.transform package, you'll see TransformerFactory. It has a newInstance() method which produces a TransformerFactory. This is a classic static factory method; read its documentation and you will see why it exists (namely to aid in producing Transformers of classes only known at run time).

Now look at the newTransformer() method. This is a factory method, but this time it's an instance method. Why? Because the TransformerFactory that the newInstance() method produced belongs to some subclass of javax.xml.TransformerFactory, a subclass which knows how to produce a Transformer of the desired class.

Confused? I wouldn't be surprised. Let's suppose that we configured things to say that the Saxon transformation package should be used. Then we first use the standard javax.xml.TransformerFactory by calling its newInstance() method. This returns an instance of a class from the Saxon product which is a subclass of javax.xml.TransformerFactory. Next we use that instance by calling its newTransformer() method, and that returns a Saxon transformer.

So why is that better than a constructor? Well, this way we write our program to do XML transformation, and later we can decide to use Saxon instead of the built-in transformer, without having to change anything. In particular, we didn't have to write a constructor to create an instance of the Saxon transformer. We didn't even need to know that Saxon existed when we wrote the program, and yet we can still decide to use it.

Gina vernon
Ranch Hand

Joined: Jan 16, 2009
Posts: 108
Paul,
Thanks for that explanation. I was able to understand how static and instance factory methods are better than constructors. I am also trying to understand the difference between static and instance factory methods so I know when to use them.
One difference is that static factory methods can return subtypes of their class while instance factory methods can't. Right/Wrong?
Second difference, static factory methods are capable of returning data types that are only known at runtime and instance factory methods are not. Right/Wrong?

Thanks.
 
Consider Paul's rocket mass heater.
 
subject: Instance factory methods