• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor is declared as private.

 
Joy Vergis
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have declared a constructor as private. The program compiles and executes also. PLease let me know in whiich possible conditions we cab have the class declared as public and constructor as private.

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think when you want your class to be used by other classes in a different package without being able to create an object for your class
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you *dont want to create an object of a class in other class* by using that constructor which is marked as private. it is similar to any other method, if you marked a method as private, then you cant see the method outside the class.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private constructors are also commonly used in the Singleton design pattern (although there is some debate on whether this is a legitimate pattern or not).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic