aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes object creation- new v/s getInstance() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "object creation- new v/s getInstance()" Watch "object creation- new v/s getInstance()" New topic
Author

object creation- new v/s getInstance()

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

We can't instantiate abstract classes because of undefined abstract method. Then why it is allowed to create object via factory method getInstance()? What is the difference between both?


Astha - OCPJP 6 (90%)
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi,

In Factory design pattern, getInstance method is supposed to create and object (or return the desired/random object from pool, depending on context).

Abstract class cannot be instantiated and hence, putting getInstance method in Abstract class is pointless(except you want to make all descendants of that class to follow getInstance() contract). getInstance of that class will return instance of a subclass (depending on conditions).

Can you be more specific (even better, provide some code where you are facing doubts)?


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
For example Calendar is abstract and Calendar.getInstance() gives you some subclass of Calendar. So Calendar can decide which subclass to return.

John
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

Thanks Anayonkar and John
John Stark wrote:For example Calendar is abstract and Calendar.getInstance() gives you some subclass of Calendar. So Calendar can decide which subclass to return.

Is this a random decision made by Calendar?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

It depends on the locale the application is running in (or the calendar is asked for). The idea is that it delivers an appropriate calendar for the context.

For example, looking at the source code for Java 1.6, Calendar.getInstance() can return one of three specific types. BuddhistCalendar if you're in Thailand, JapaneseImperialCalendar if you're in Japan, or GregorianCalendar otherwise. I haven't looked at the Java 7 code to see if they've added any more.
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

Matthew Brown wrote:It depends on the locale the application is running in (or the calendar is asked for). The idea is that it delivers an appropriate calendar for the context.

For example, looking at the source code for Java 1.6, Calendar.getInstance() can return one of three specific types. BuddhistCalendar if you're in Thailand, JapaneseImperialCalendar if you're in Japan, or GregorianCalendar otherwise. I haven't looked at the Java 7 code to see if they've added any more.

Is it like this-

and


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object creation- new v/s getInstance()