mercredi 24 juin 2015

Implement singleton with static acces modifier

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?

Aucun commentaire:

Enregistrer un commentaire