aspose file tools*
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 Java 8 in Action this week in the Java 8 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: 23

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
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

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

Joined: Dec 21, 2010
Posts: 23

Thanks Paul.. That was informative..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private Constructor in Java
 
Similar Threads
constructor doubt
Java constructore
Construtor rules
Constructor Rules
Need help to understand constructor marked private?