Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. How not to create an instance of a singleton

How not to create an instance of a singleton

Scheduled Pinned Locked Moved The Weird and The Wonderful
java
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dennis_E
    wrote on last edited by
    #1

    I came across this java code. (I changed the class name to SingletonClass)

    private static SingletonClass instance;

    public SingletonClass()
    {
    SingletonClass.instance = this;
    }

    public static SingletonClass getInstance()
    {
    return instance;
    }

    N M B 3 Replies Last reply
    0
    • D Dennis_E

      I came across this java code. (I changed the class name to SingletonClass)

      private static SingletonClass instance;

      public SingletonClass()
      {
      SingletonClass.instance = this;
      }

      public static SingletonClass getInstance()
      {
      return instance;
      }

      N Offline
      N Offline
      Nicholas Marty
      wrote on last edited by
      #2

      Singleton... public constructor... I don't see anything wrong here :rolleyes: Oh... And I'm curious what you do when you don't call the constructor at least once :laugh:

      1 Reply Last reply
      0
      • D Dennis_E

        I came across this java code. (I changed the class name to SingletonClass)

        private static SingletonClass instance;

        public SingletonClass()
        {
        SingletonClass.instance = this;
        }

        public static SingletonClass getInstance()
        {
        return instance;
        }

        M Offline
        M Offline
        Marc Clifton
        wrote on last edited by
        #3

        Which of course is used like this: SingletonClass myVerySingletonClass = (new SingletonClass()).getInstance(); or even better:

        SingletonClass anInstanceThatWeDontUse = new SingletoneClass();
        SingletonClass useThisInstead = anInstanceThatWeDontUse.getInstance();

        Good stuff! :laugh: On the other hand, I actually have written code similar to this, except I don't call it a singleton of course. Granted, it's smelly code. Quiz: What might be the reason to provide a static method that returns an instance of the class, when you know that there will ever only be one instance? Marc

        N B 2 Replies Last reply
        0
        • D Dennis_E

          I came across this java code. (I changed the class name to SingletonClass)

          private static SingletonClass instance;

          public SingletonClass()
          {
          SingletonClass.instance = this;
          }

          public static SingletonClass getInstance()
          {
          return instance;
          }

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          That's just an example of the well-known Renewable Singleton Pattern. That comes in very handy when the old instance has worn out. For a closer description, see

          "Real World Software Development, Vol. II: Design Patterns", by W.T. and F. (2021)

          OriginalGriffO 1 Reply Last reply
          0
          • M Marc Clifton

            Which of course is used like this: SingletonClass myVerySingletonClass = (new SingletonClass()).getInstance(); or even better:

            SingletonClass anInstanceThatWeDontUse = new SingletoneClass();
            SingletonClass useThisInstead = anInstanceThatWeDontUse.getInstance();

            Good stuff! :laugh: On the other hand, I actually have written code similar to this, except I don't call it a singleton of course. Granted, it's smelly code. Quiz: What might be the reason to provide a static method that returns an instance of the class, when you know that there will ever only be one instance? Marc

            N Offline
            N Offline
            Nicholas Marty
            wrote on last edited by
            #5

            SingletonClass useThisInstead = anInstanceThatWeDontUse.getInstance();

            This would actually generate a compile error as it getInstance() is a static method ;P

            M D 2 Replies Last reply
            0
            • N Nicholas Marty

              SingletonClass useThisInstead = anInstanceThatWeDontUse.getInstance();

              This would actually generate a compile error as it getInstance() is a static method ;P

              M Offline
              M Offline
              Marc Clifton
              wrote on last edited by
              #6

              Nicholas Marty wrote:

              This would actually generate a compile error as it getInstance() is a static method

              :doh: Marc

              1 Reply Last reply
              0
              • B Bernhard Hiller

                That's just an example of the well-known Renewable Singleton Pattern. That comes in very handy when the old instance has worn out. For a closer description, see

                "Real World Software Development, Vol. II: Design Patterns", by W.T. and F. (2021)

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                Ah yes! Code Entropy!

                Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                L 1 Reply Last reply
                0
                • M Marc Clifton

                  Which of course is used like this: SingletonClass myVerySingletonClass = (new SingletonClass()).getInstance(); or even better:

                  SingletonClass anInstanceThatWeDontUse = new SingletoneClass();
                  SingletonClass useThisInstead = anInstanceThatWeDontUse.getInstance();

                  Good stuff! :laugh: On the other hand, I actually have written code similar to this, except I don't call it a singleton of course. Granted, it's smelly code. Quiz: What might be the reason to provide a static method that returns an instance of the class, when you know that there will ever only be one instance? Marc

                  B Offline
                  B Offline
                  BobJanova
                  wrote on last edited by
                  #8

                  If there'll only ever be one instance, that's your typical singleton, isn't it? I don't really understand your quiz question. I've done load-on-demand singletons in the past which may be what the doofus in charge of this example was going for, I suppose.

                  1 Reply Last reply
                  0
                  • N Nicholas Marty

                    SingletonClass useThisInstead = anInstanceThatWeDontUse.getInstance();

                    This would actually generate a compile error as it getInstance() is a static method ;P

                    D Offline
                    D Offline
                    Dennis_E
                    wrote on last edited by
                    #9

                    Actually, in Java you are allowed to call a static method from an instance... One of many dumb things Java does.

                    N 1 Reply Last reply
                    0
                    • D Dennis_E

                      Actually, in Java you are allowed to call a static method from an instance... One of many dumb things Java does.

                      N Offline
                      N Offline
                      Nicholas Marty
                      wrote on last edited by
                      #10

                      Uh... Somehow I mistook the code for c#... Somehow skipped that in the original post :~ At least in c# it isn't possible to do that and I'd think even Java should at least give a compiler warning about that ;)

                      1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        Ah yes! Code Entropy!

                        Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                        L Offline
                        L Offline
                        Lutoslaw
                        wrote on last edited by
                        #11

                        It makes sense. Every "real-world" (i.e. big) JAVA application reassures me that java is nondeterministic.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups