• 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

About private constructor...

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's said in the K&B's book,


Constructor can use any access modifiers, including private. (A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to an instance created within the class



I don't have c clear understand in that bold para. And see the below coding....



makes a Compilation error as I expected [ClassA(int) has private access in ClassA]. What is the solution? Thanks in Advanced!
 
Ranch Hand
Posts: 504
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this means that if the constructor of a class is private it can only be instantiated within that class and none of its subclass will be able to call that private constructor and hence none of the subclass can be instantiated with call to that private constructor.

In your code you can change the modifier of constructor from private to protected this way your subclass will be able too call that constructor.

 
Ranch Hand
Posts: 317
Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This means that if you wants to create an instance of the class with private constructor, you have to provide a mean to initiate that class from within the class itself.

In your class you have to provide a static method which will return object of it's own class like




and you can use this class as:




Classes with private constructor are useful when you don't want a class to be extended by some other class Or when you want to create a class as singleton.

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Neha Daga. Thanks a lot Harpreet Singh janda, Great! Actually, I'm typing those questions, but explained it! Thanks....
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny 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