This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help to understand constructor marked private?

 
rizwana mujawar
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They say "Constructor can use any modifier including private. A private constructor means only code within the same class itself can instantiate an object of that type,so if private constructor class wants to allow instance of the class to be used,the class must provide a static method or a variable that allows access an instance created from within the class ".
I dint get the above statement.. Can anyone help me to understand this statement. Please...!
Thanks
 
John Jai
Rancher
Posts: 1776
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A private constructor means only code within the same class itself can instantiate an object of that type

Sample -

the class must provide a static method or a variable that allows access an instance created from within the class

Sample -
 
Md. Minhajur Rahman
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A private constructor means only code within the same class itself can instantiate an object of that type,so if private constructor class wants to allow instance of the class to be used, the class must provide a static method or a variable that allows access an instance created from within the class ". because static method or variable is the only way as only static members are accessible before creating any instance of any class. As constructor is private, so you can't create an instance of that class at outside of the class, so can't access any instance method. So Static member is the way if private constructor class wants to allow instance of the class to be used.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic