• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

factory methods???????

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One question, I found in Master Exam, asking about classes instantiated by factory methods.
Anyone please, please define factory method.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Factory methods are methods which creates instance of a particular class.
 
Arijit Daripa
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chander....
Thanks a lot....
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Arijit Daripa
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 9704
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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...
 
Arijit Daripa
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit.......
That mean every getInstance() method is a factory method.
 
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic