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

Can constructors be declared private:If so whar are the advatages

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can constructors be declared private and if so what are the
advantages?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It helps in preventing the instantiation of a class
This is a good way of preventing instances from being created than declaring the class as abstract
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pravin
The best example is Math class which has private constructor.
The abstract class and a class having private constructor are not one and the same.
* Abstract classes have to be exteneded to get the functionality.In case of classes having private constructor,you can't instantiate it directly,but you can call static method (if any exists).which gives the objet.

HTH
Prasad Ballari

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Imagine you need a class in which you want to restrict the number of instances it can. In this situation you can have your constuctor 'private' so that methods which instantiating will not perform it directly, but you can provide a static method inside the class which returns the instance of the same based on some 'static' counter in it.
I hope this will help you.....
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Typically it helps when you want a singleton object pattern (where you create just one object for the whole application like maybe a database connection pool manager).
What you do is that you create a private constructor, and then give public access to a method called say getinstance(). In this method, you check if there already an instance created, if so return the refernce to the instance, if not, cerate an instance and return the refernce to it...
HTH,
Shubhangi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic