• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Query on constructor

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can a constructor of a class be marked as private?
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
up to my knowledge you can mark as private.but,you can't instantiate that class.this is mainly used in singleton creation
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have two cases here.
1)you can instantiate the class with it.
2)but, you either cannot extend the class nor instantiate it outside the class

i think this helps you..??
 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, i should have told like,
you can instantiate it within the same class(ie. class with constructor marked as private)
 
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since a constructor (although private) has been defined in the superclass, the base class cannot access it, hence even though the base class has a default constructor, it cannot make an implicit call to the constructor in the superclass (since it is private). Any constructor must have a call to a constructor in the supercalss as its first statement.
So effectively what we are doing is trying to access the private member of the superclass which is illegal in the first place.
The class by itself can be instantiated with a private constructor, there are no problems.
 
Marshal
Posts: 75653
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can have a private constructor. It is used for two main reasons:
  • To prevent the class being instantiated at all.
  • To restrict the class from being instantiated outside a factory method. Do a search for singleton pattern, which uses that trick.
  • If you have a class which only has private constructors, it is impossible to inherit from that class, so you ought to mark the class "final" as well.
     
    The human mind is a dangerous plaything. This tiny ad is pretty safe:
    free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic