Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Private Constructors

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all ,
i have studied that we can even make constructor private and by providing a static method which returns the instance of the class we can create objects of the class(i.e factory methods) . but my point is why we are going for this concept of factory methods and private constructor .


thanks in advance

ashok
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're probably talking about singletons. Check the following in the FAQ :
http://faq.javaranch.com/view?SingletonPattern
 
ashok ganesan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ya got the logic
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's lots of reasons why you might prefer a static factory method to a constructor. I really recommend Joshua Bloch's excellent book, "Effective Java". His first tip is to "consider providing static factory methods instead of constructors". He cites some advantages:

* You can name them descriptively -- this can be much more helpful to users of your API than multiple constructors
* You don't have to create a new object; you can reuse old ones. The Singleton, mentioned above, is one example.
* You can return objects of any subtype of the declared return type.

And as disadvantages:
* You can't subclass a class that does not have visible constructors.
* Static factory methods don't stand out as much in API documentation (e.g. JavaDoc).
 
Look ma! I'm selling my stuff!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic