• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private Constructor

 
Vivek Alampally
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Kindly explain me, what exactly author is trying to explain with example in the following paragraph
"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.)"

Thanks
Vivek
 
Siddhesh Deodhar
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this Link
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
constructor is a special kind of method , you can specify an access specifier[what are all applicable to a method] to a constructor. so you can not access the *particular* constructor of that class[say class A] from other class[say class A]. and if you want to instantiate A from B then, provide a public method[Factory method] in A which create an object of A[private scope is available inside a class], and access the method from class B. this technique is useful in some core design pattern like singleton pattern, strategy pattern etc...

hth
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:constructor is a special kind of method


No it isn't. A constructor is most emphatically NOT a method.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:A constructor is most emphatically NOT a method.

Yes, you are right; that is why I mentioned as *special kind* of method
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that implies constructors are methods, which they aren't.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic