File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes i have a private constructor and a public constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "i have a private constructor and a public constructor" Watch "i have a private constructor and a public constructor" New topic
Author

i have a private constructor and a public constructor

slagy maggie
Ranch Hand

Joined: Feb 20, 2001
Posts: 35
Hi,
I have a class, which has one private constructor
public class test extends test1
{
private test{
super();
}
and then a public test(int i, String s){blah blah}
now if i wanted a singleton model, and did not want 2 instances of thsi class, why have a public constructor(that will allow other classes to create other instances ???/!!!11)
and moreover that super(), in the private constructor accesses
the public constructor in test1 class. what all this lead to???does it all not lead to totally counteracting the need a for a private constructor???
thanks guys
Doug Gschwind
Greenhorn

Joined: Sep 17, 2000
Posts: 27
Actually, the singleton model does NOT expose a public constructor. An example is the following:
public class Singleton
{
private static Singleton instance;
private Singleton()
{
super();
}
public static synchronized Singleton getInstance()
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
// other stuff below
}
The call to super() from the private constructor exposes nothing additional to Singleton's consumers. The code above also limits construction to just one instance. Consumers of this class only obtain access to the one instance via the getInstance() class method.
 
 
subject: i have a private constructor and a public constructor
 
Similar Threads
doubt
Constructors/Strings HELP
A class cannot be subclassed...
can we create in the same class
I know high level classes can not be declared private but...