Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Private Constructors

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
thanks ya got the logic
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • 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).
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic