Do I have to use private constructor to make a class singleton? Is there any other way than using private constructor? Can't I make a class singleton using public constructor?
-
1possible duplicate of Why is it mandatory to have private Constructor inside a Singleton classAbaddon666– Abaddon6662015-09-17 10:37:27 +00:00Commented Sep 17, 2015 at 10:37
-
@ManikantaMani You may take a look at my answer below.user3437460– user34374602015-09-17 17:47:46 +00:00Commented Sep 17, 2015 at 17:47
6 Answers
The reason it has to be a private constructor is because you want to prevent people from using it freely to create more than one instance.
A public constructor is possible only if you are able to detect an instance already exist and forbid a another instance being created.
Is there no way other than using private constructor ? Can't we make a class singleton using public constructor ?
Yes, it is actually possible, for example:
class Singleton
{
private static Singleton instance = null;
public Singleton() throws Exception //Singleton with public constructor
{
if(instance != null)
throw new Exception("Instance already exist");
//Else, do whatever..such as creating an instance
}
}
1 Comment
One way to create Singleton object using public constructor with throw exception from constructor if object is already exist.
For Example:
public class Singleton {
private static Singleton singleton;
private String hello;
public Singleton() throws Exception {
if (singleton != null) {
throw new Exception("Object already exist");
}
}
public static Singleton getInstance() throws Exception {
if (singleton == null) {
singleton = new Singleton();
}
return singleton;
}
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
public static void main(String[] args) {
try {
Singleton single1 = Singleton.getInstance();
single1.setHello("Hello");
System.out.println(single1.getHello());
Singleton single2 = Singleton.getInstance();
System.out.println(single2.getHello());
Singleton single3 = new Singleton();
System.out.println(single3.getHello());
} catch (Exception ex) {
}
}
}
1 Comment
Generally, Singleton pattern is used when you should have exactly 1 object of a class (e.g. Factories are very often implemented as Singletons).
public modifier allows anyone to access your method (as well as your constructor). In that case, anyone can create an instance of your class, thus it won't be a singleton anymore.
By setting private modifier to your constructor, you are sure, it cannot be used outside of the class - no one will create any more objects of that class.
public class SingletonExample {
private static final SingletonExample instance;
// Initialize instance object in static block - done while loading the class by class loader
static {
instance = new SingletonExample();
}
private SingletonExample(){}
public static SingletonExample getInstance(){
return instance;
}
// Method used to create java.test.DateFormat using Oracle SQL Date pattern
public DateFormat createDateFormat(String mask){
if("DD-MM-YYYY HH24:MI:SS".equals(mask)){
return new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
} else if("DD-MM-YYYY".equals(mask)){
return new SimpleDateFormat("dd-MM-yyyy");
} else {
throw new UnsupportedOperationException("Unsupported mask - " + mask);
}
}
}
And usage example
public class SingletonExampleTest{
public static void main(String... args){
java.util.Date date = Calendar.getInstance().getTime();
SingletonExample singleton = SingletonExample.getInstance();
String mask = "DD-MM-YYYY";
DateFormat df = singleton.createDateFormat(mask);
System.out.println(df.format(date));
}
}
Comments
The private constructor is a reason, why your class is Singleton.
This Example shows pretty well, how you can use your Singleton class by using getInstance()