6 Replies Latest reply: Jul 3, 2012 5:01 AM by EJP RSS

    Static inner class over Singleton

    800839
      Hi,

      Whether we can implement singleton using static inner class? What is the advantage of doing so and what are the pros and cons of it?
      Because Static inner class have restriction as below:-

      1. A singleton class can extends class and implement interfaces , while Static class can not.
      2. A singleton can be instantiated lazily or asynchronously while a static class is generally initialized when it is first loaded.
      3.A singleton class can extended and its methods can be overridden.
      4. The most important advantage is the singleton can be handled polymorphic ally without forcing their users to assume that there is only one instance.

      Hence why need to go for a Static inner class over singleton?
        • 1. Re: Static inner class over Singleton
          gimbal2
          797836 wrote:
          Hence why need to go for a Static inner class over singleton?
          You don't need to do anything; you do what makes sense to you. And that might be going as far as not needing a singleton at all.

          Let me put it differently: you're not going to be punished by programming gods if you go one way and a general group of programmers that think they have the right to dictate what's a "best practice" goes another way. There is only one thing that matters: that you and your colleagues can read and maintain the code (and that it works of course).
          • 2. Re: Static inner class over Singleton
            800839
            public class Singleton {

            // Prevent instantiation by public.
            private Singleton() {
            }

            public static Singleton getInstance() {
            return SingletonHolder.instance;
            }

            /**
            * Singleton implementation helper.
            */
            private static class SingletonHolder {
            // This gets initialized on first reference
            // to SingletonHolder.
            static final Singleton instance = new Singleton();
            }
            }

            The above code is based on Initialize-on-demand holder class idiom . This method relies on the JVM only intializing the class members upon first reference to the class. In this case, we have a inner class that is only referenced within the getInstance() method. This means SingletonHolder will get initialized on the first call to getInstance().Now the singleton object will not get allocated until it is used and we did not need to incur the overhead of a synchronized method call.
            Hence this does have some advantage compared to lazy or eager initialization.

            Thanks.
            • 3. Re: Static inner class over Singleton
              EJP
              Hence this does have some advantage compared to lazy or eager initialization.
              It is lazy instantation.
              • 4. Re: Static inner class over Singleton
                Tolls
                797836 wrote:
                public class Singleton {

                // Prevent instantiation by public.
                private Singleton() {
                }

                public static Singleton getInstance() {
                return SingletonHolder.instance;
                }

                /**
                * Singleton implementation helper.
                */
                private static class SingletonHolder {
                // This gets initialized on first reference
                // to SingletonHolder.
                static final Singleton instance = new Singleton();
                }
                }

                The above code is based on Initialize-on-demand holder class idiom . This method relies on the JVM only intializing the class members upon first reference to the class. In this case, we have a inner class that is only referenced within the getInstance() method. This means SingletonHolder will get initialized on the first call to getInstance().Now the singleton object will not get allocated until it is used and we did not need to incur the overhead of a synchronized method call.
                Hence this does have some advantage compared to lazy or eager initialization.

                Thanks.
                Even if the singleton instance was a member of the Singleton class rather than an inner holding class it would still not be initialised until getInstance() was called (barring some forced class loading).
                Unless there is some other public static data or method then this technique is a little pointless, outside of some edge case or other.
                • 5. Re: Static inner class over Singleton
                  mlvenkatesh
                  Best i would go fo a static singleton would be for a class with utility methods
                  • 6. Re: Static inner class over Singleton
                    EJP
                    Whatever a 'static singleton' might be.