4

Example class with singleton design pattern.

  class Singleton {
        private static Singleton instance;
        private int x;

        private Singleton() {
             x = 5;
        }

        public static synchronized Singleton getInstance() {
            if(instance == null) {
                instance = new Singleton();
            }
            return instance;
        }
            public void doSomething() {
                System.out.println("Hello");
        }
  }

I'm just wondering can I create this class with same variables and methods declared as static. Is it same as the singleton?

Jon
  • 2,932
  • 2
  • 23
  • 30
Aisha Swan
  • 41
  • 6

3 Answers3

0

Singleton should be considered only if all three of the following criteria are satisfied:

  1. Ownership of the single instance cannot be reasonably assigned
  2. Lazy initialization is desirable
  3. Global access is not otherwise provided for

Yes, It is the same.

Gul Ershad
  • 1,743
  • 2
  • 25
  • 32
0

If you really need to implement a singelton pattern I would recommend using an enum:

public enum MySingelton{
 INSTANCE;
   private final String[] variable = new String[]{"test", "test2};
 public void randomFunction(){     
 }
}

Call it with:

MySingelton.INSTANCE.randomFunction();

With an enum implementation it's guaranteed that only one instance is created and that it's available all the time. Also it's possible to serialize and deserialize the singelton without creating multiple copies of it.

More information can be found here:

What is an efficient way to implement a singleton pattern in Java?

http://www.drdobbs.com/jvm/creating-and-destroying-java-objects-par/208403883?pgno=3

Community
  • 1
  • 1
davidgiga1993
  • 2,695
  • 18
  • 30
0

Since the purpose of the singleton pattern is to ensure that a single instance of a class exists, yes, you could use static members to achieve the same effect.

So instead of

public class Singleton {
    private static Singleton theInstance = new Singleton();
    private int aVar = 10;
    public void aMethod() { 
        System.out.println(aVar);
    }
    public static Singleton getInstance() {
        return theInstance;
    }
}

you could do

public class FakeSingleton {
    private static int aVar = 10;
    public static void aMethod() {
        System.out.println(aVar);
    }
}

and have exactly the same functionality (instead of Singleton.getInstance().aMethod() you would write FakeSingleton.aMethod()).

Using the singleton pattern can be advantageous if you want lazy initialization, so that the singleton is only initialized when it is first needed, as follows:

public class Singleton {
    private static Singleton theInstance = null;
    private int aVar = 10;
    public void aMethod() { 
        System.out.println(aVar);
    }
    public static Singleton getInstance() {
        if (theInstance == null) {
            theInstance = new Singleton();
        }
        return theInstance;
    }
}

(Note that the above is not thread-safe, in multithreaded code you will need to add synchronization.)

Hoopje
  • 12,677
  • 8
  • 34
  • 50