Java Singleton Pattern

  • + 1 comment

    IMHO, @dvt32's solution is the best. You can avoid checking "instance == null" and meanwhile create a thread safe Singleton instance during class loading. Alternatively, you can still use lazy initialization thread safe Singleton, using synchronized. Find more from my post

    class Singleton {
        private Singleton() {}
        public String str;
        private static final Singleton instance = new Singleton();
        public static Singleton getSingleInstance() { 
            return instance; 
        }
    }