dimanche 20 août 2017

Why would someone use Singleton Design Pattern instead of making all the fields/methods static? [duplicate]

This question already has an answer here:

Singleton:

public class Test{
   private Test singleInstance = new Test();
   private int someVariable;

   private Test(){
   }

   public Test getInstance(){
      return this.singleInstance;
   }

   public void someMethod(){
      //do nothing
   }
}

Using static on fields/methods:

public class Test{
       public static int someVariable;

       private Test(){
       }

       public static void someMethod(){
          //do nothing
       }
}

Having static on all the fields/methods will make the class behave like one instance of an object. Is there any reason for using Singleton instead?

Aucun commentaire:

Enregistrer un commentaire