• Post Reply Bookmark Topic Watch Topic
  • New Topic

Private Constructor  RSS feed

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you use a private constructor?

Thanks, Lisa
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

Classical situations are when you want to create a Singleton or when you want to create an utility class that only provides services and should not be instantiated (take a look at java.lang.Math).
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typesafe enumerators prior to J2SE 5.0 are another use for private constructors. See "Type-Safe Enumerations" for an overview. Also "Effective Java" (Bloch) Item 21 if you have that excellent book.
Cheers,
Simon
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!