This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need help to understand constructor marked private? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need help to understand constructor marked private?" Watch "Need help to understand constructor marked private?" New topic
Author

Need help to understand constructor marked private?

rizwana mujawar
Ranch Hand

Joined: Feb 24, 2012
Posts: 36
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Apr 10, 2012
Posts: 33
"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.
 
wood burning stoves
 
subject: Need help to understand constructor marked private?