lundi 21 décembre 2015

Why make singleton object private instead of public

In Java you can create a singleton like this:

public class SingleObject {

   //create an object of SingleObject
   private static SingleObject instance = new SingleObject();

   //make the constructor private so that this class cannot be
   //instantiated
   private SingleObject(){}

   //Get the only object available
   public static SingleObject getInstance(){
      return instance;
   }
}

Is there any reason why you just couldn't make instance public and dispense with getInstance?

Aucun commentaire:

Enregistrer un commentaire