This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Private Constructor in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Private Constructor in Java" Watch "Private Constructor in Java" New topic
Author

Private Constructor in Java

Jean John
Greenhorn

Joined: Dec 21, 2010
Posts: 24

Hi folks,
I read about the possibility of a Private Constructor in Java.

Constructors can use any access modifier, 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 be used, the class must provide a static method or variable that allows access to an instance created from within the class.)


Is there any practical scenario where this can be put to use?
Why would anyone possibly want a static method that allows access to an instance created from within the class?

Thanks
Jean
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19739
    
  10

Plenty of reasons. Read this Wikipedia article to see some of them: Factory method pattern.
Jean John
Greenhorn

Joined: Dec 21, 2010
Posts: 24

Thanks Paul.. That was informative..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private Constructor in Java
 
It's not a secret anymore!