*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes factory methods??????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "factory methods???????" Watch "factory methods???????" New topic
Author

factory methods???????

Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
One question, I found in Master Exam, asking about classes instantiated by factory methods.
Anyone please, please define factory method.


SCJP 5
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
class Test
{
private Test t = new Test();
private Test(){}
public static Test getInstance()
{
return t;
}
}

In this example getInstance is factory method.
factory method is just a fancy name. Any method which create or return object of the class can be factory method.

Please let me know If I am wrong somewhere.
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
Originally posted by Brij garg:
Any method which create or return object of the class can be factory method.

Thanks brij. I will remember the above quote.
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Factory methods are methods which creates instance of a particular class.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
Thanks Chander....
Thanks a lot....
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Factory methods are not part of the exam. To add to that the Factory pattern by itself is split into the Abstract Factory pattern and the Factory method pattern. The details however are not needed for the SCJP


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
Originally posted by Deepak Bala:
Factory methods are not part of the exam. To add to that the Factory pattern by itself is split into the Abstract Factory pattern and the Factory method pattern. The details however are not needed for the SCJP


But there was a question in Master Exam, asking, "Which of the following classes are instantiated by factory methods?" That's why I asked the definition.
Well... Thanks for the information
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

@Brij garg your code has a mistake, it will not compile as you are accessing a non-static field from a static method.



@arijit, you can understand this as follows

If you want that no one can create an instance of your class, and you provide a method that provides instances of that class, that method is called factory method. So basically a factory method returns an instance of its own class.

To do this you will have to create all the constructors of the class private and have to create a static public method, that returns an instance of that class. The method might return new instance every time or might return the same instance all the time as in the example by brij.

There are many reasons for this. Your class might be incomplete in itself like the Calendar class, which has a getInstance method which returns an instance of GregorianCalendar class usually. Other reason might be that you want that only one instance of the class be created on one JVM as shown in the code by brij...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
Thanks Ankit.......
That mean every getInstance() method is a factory method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: factory methods???????
 
Similar Threads
Factory Class??
What is a Factory method please explain with use of such methods
static methods for object creation
Factory Methods
factory methods