mardi 10 janvier 2017

Private constructor in singleton [duplicate]

This question already has an answer here:

 public class DBSingleton {

 private static DBSingleton instance = new DBSingleton();

 private DBSingleton() {
 // why do we need this constructor
 }
 public DBSingleton getInstance() {
     return instance;
 }
}

How private constructor can help me?

I mean all variables and methods are static and even if I leave constructor public it will not change anything.
lets say someone create an object of my singleton nothing will happen because everything inside this class is static which means associated with the class itself not with object. Java static property is shared to all objects.

can anyone give me reasonable example please

Aucun commentaire:

Enregistrer un commentaire