• Post Reply Bookmark Topic Watch Topic
  • New Topic

Private Constructor in Java

 
Jean John
Greenhorn
Posts: 24
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21973
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plenty of reasons. Read this Wikipedia article to see some of them: Factory method pattern.
 
Jean John
Greenhorn
Posts: 24
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.. That was informative..
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!