-7

This answer says that we can't instantiate more than one object at a time via private constructors. I have modified the code which does just the opposite:

class RunDatabase{
    public static void main(String[] args){
        Database db = Database.getInstance("hello");//Only one object can be instantiated at a time
        System.out.println(db.getName());
        Database db1 = Database.getInstance("helloDKVBAKHVBIVHAEFIHB");
        System.out.println(db1.getName());
    }
}
class Database {

    //private static Database singleObject;
    private int record;
    private String name;

    private Database(String n) {
        name = n;
        record = 0;
    }

    public static synchronized Database getInstance(String n) {
        /*if (singleObject == null) {
            Database singleObject = new Database(n);
        }

        return singleObject;*/

        return new Database(n);
    }

    public void doSomething() {
        System.out.println("Hello StackOverflow.");
    }

    public String getName() {
        return name;
    }
}

And as expected both the strings are being printed. Did I miss something?

Community
  • 1
  • 1
piepi
  • 141
  • 1
  • 4
  • 12
  • 2
    yeah, you missed the part where your statment (question), exlusivly refered to a `singleton`. – SomeJavaGuy Oct 13 '16 at 11:19
  • The answer does not say such a thing. Private constructors are merely a way of enforcing the singleton pattern. You only cheat it by having your main inside that class. – f1sh Oct 13 '16 at 11:20
  • Making the constructor `private` is one of the things you need to do to implement the singleton pattern. But it's not the only thing, and just this by itself does not automatically make it a singleton. – Jesper Oct 13 '16 at 11:29
  • So many down-votes, should I delete the question? – piepi Oct 13 '16 at 11:37
  • @piepi, you shouldn't, you have already got 3 good answers – Andrew Tobilko Oct 13 '16 at 11:47

3 Answers3

3

We can't instantiate more than one object at a time via private constructors.

No, we can. A private constructor only avoids instance creation outside the class. Therefore, you are responsible for deciding in which cases a new object should be created.

And as expected both the strings are being printed. Did I miss something?

You are creating a new instance every time the getInstance method is called. But your commented code contains a standard lazy initialization implementation of the singleton pattern. Uncomment these parts to see the difference.

Other singleton implementations you can look over here.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
2

Private constructors are used to stop other classes from creating an object of the "Class" which contains the private constructor.

This is done to provide the singleton pattern where only a single instance of the class is used.

The code that you have mentioned is supposed to create just one instance of the class and use that instance only to perform all the operations of that class. But you have modified the code which violates the singleton design pattern.

Vishal
  • 21
  • 2
1

Why do we need a private constructor at all?

Basically 3 reasons:

  1. if you don't want the user of the class creates an object directly, and instead use builders or similars,

  2. if you have a class defined for constants only, then you need to seal the class so the JVM don't create instances of the class for you at runtime.

  3. singletons

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97