• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Private Constructor

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do u need Private modifier for Class constructors???

[This message has been edited by satish (edited April 18, 2000).]
 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't it more accurate to say you don't want someone to directly instantiate the class using a given constructor? Couldn't you have a public method within the class that instantiates the class if you wanted to?
The powers that be cleaned the jdk off the terminal I use at work (don't tell them it was me!) so I'll have to test it another time...seems right though...
Eric
 
Eric Barnhill
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Follow up - I got hold of another jdk. Here is the code:
<pre>
public class PrivateConst {
private PrivateConst() {
System.out.println("in private constructor");
}
public PrivateConst(int n) {
System.out.println("other public constructor " + n);
}
public void giveMePrivate()
{
PrivateConst pc = new PrivateConst();
}
}
---- in another file ----
public class Requestor1 {
public static void main (String[] args) {
PrivateConst pc1 = new PrivateConst(3);
PrivateConst pc2 = new PrivateConst();
}
}
---- in third file ---
public class Requestor2 {
public static void main (String[] args) {
PrivateConst pc1 = new PrivateConst(3);
pc1.giveMePrivate();
}
}
---- output ----
class PrivateConst compiles
class Requestor1 : doesn't compile, cites "no constructor matching PrivateConst() found in class PrivateConst
class Requestor2 : compiles
output of runnning "java Requestor2" :
other public constructor 3
in private constructor

----
</pre>
hope this helps someone!
Eric B.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For those intrested, I know that previously there
was a discussion on the ranch about this and it is
true (as other pointed out) that constructors can
be private. In which case, we need a more accessible
method which can instantiate the class for the caller.
I donot want to elaborate but, a search may be worth
while......
Regds.
- satya
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you don't necessarily need a method which can instantiate, if you don't need to instantiate the class. Consider the Math class - all its methods are static, so an instance is never needed. Which is why there is no way provided for you to ever instantiate a Math object - the constructor is private, and no method is provided to give you an instance.
Alternately, look at the Runtime class. It also has a private constructor. The only way to get a reference to a Runtime object is through the static method Runtime.getRuntime(). It doesn't create an instance exactly - it just gives you a reference to an instance which was created when the JVM first started up.
 
satish
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic