Results 1 to 20 of 20
  1. #1
    esha is offline Member
    Join Date
    Jan 2014
    Posts
    8
    Rep Power
    0

    Default To Obtain Explanation on Singleton Implementation

    Hello,

    I've found following code for Singleton Implementation from this forum, I am having a difficulty of understanding following,

    1. When Singleton instance will be created, ( From which call ) ?
    2. What feature of Static Inner class allow object to be singleton ?
    3. How Inner Class implementation is thread safe ?
    4. What will happen if variable "instance" mark as non-static, Will it still grantee singleton implementation ?

    Appreciate if some one can help me to understand this. Thanks in Advance.

    Java Code:
    public class Singleton {
       // Note private constructor
       private Singleton() {}
     
       private static class SingletonInner {
         private final static Singleton instance = new Singleton();
       }
     
       public static Singleton getInstance() {
         return SingletonInner.instance;
       }
     }
    Last edited by esha; 01-11-2014 at 05:42 PM.

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: To Obtain Explanation on Singleton Implementation

    This is called Lazy initialization on demand holder idiom.

    The instance will be created when getInstance is called.
    I just know that it is thread safe without needing to use volatile or synchronized.
    Instance cannot be static because you cannot make a static reference to a non static variable and it needs to be common to the class

    http://en.wikipedia.org/wiki/Initial...d_holder_idiom it explains here how it works
    Last edited by superhaNds; 01-11-2014 at 06:05 PM.

  3. #3
    esha is offline Member
    Join Date
    Jan 2014
    Posts
    8
    Rep Power
    0

    Default Re: To Obtain Explanation on Singleton Implementation

    Thanks.
    What I understand from your explanation is that "SingletonInner.instance" will create the instance object. Please correct me if I am wrong.
    What will happen if "getInstance()" method calls again ? Unfortunately I cant identify any difference how first call will create the instance and from next call onward already created instance will be returned. Is it because "instance" marked as final ?

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: To Obtain Explanation on Singleton Implementation

    the static class SingletonInner will be executed only when getInstance() is called, and once this happens the inner class is initialized, but not before.
    Yes, when that is executed then the inner class will be initialized, thus creating instance.

    because the variable is static, so every getInstance() call with return the same.

    Read from the wiki article as well.

  5. #5
    esha is offline Member
    Join Date
    Jan 2014
    Posts
    8
    Rep Power
    0

    Default Re: To Obtain Explanation on Singleton Implementation

    Many Thanks ! I've read the wiki as well.

    One last question, Is that correct if I say "instance" variable does not required to be final ?

  6. #6
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: To Obtain Explanation on Singleton Implementation

    I don't think it would cause problems, not completely sure though. But the the value of instance should not be changed after the initialization, therefore final makes sense.

  7. #7
    esha is offline Member
    Join Date
    Jan 2014
    Posts
    8
    Rep Power
    0

    Default Re: To Obtain Explanation on Singleton Implementation

    Thanks ! Yes Agree.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,512
    Blog Entries
    7
    Rep Power
    20

    Default Re: To Obtain Explanation on Singleton Implementation

    Quote Originally Posted by superhaNds View Post
    I just know that it is thread safe without needing to use volatile or synchronized.
    That's an extremely weak argument; it's because class loading is done in a thread safe way (per classloader) and a class is only initialized when something refers to it for the first time; the sketched scenario is only hiding synchronization.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: To Obtain Explanation on Singleton Implementation

    Quote Originally Posted by JosAH View Post
    That's an extremely weak argument; it's because class loading is done in a thread safe way (per classloader) and a class is only initialized when something refers to it for the first time; the sketched scenario is only hiding synchronization.

    kind regards,

    Jos
    That's nice.

    but it wasn't an argument. I am not arguing for anything, that would be totally different.

    I have read that it is thread safe, so I was stating a fact.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,512
    Blog Entries
    7
    Rep Power
    20

    Default Re: To Obtain Explanation on Singleton Implementation

    Quote Originally Posted by superhaNds View Post
    That's nice.

    but it wasn't an argument. I am not arguing for anything, that would be totally different.

    I have read that it is thread safe, so I was stating a fact.
    Maybe it wasn't an argument, but it was an extremely weak explanation; saying that you 'just know something' is sophism or proof by intimidation and taking something for a fact because you have 'read it' has caused wars and religions in the past. Explaining something in a forum takes a bit of reasoning and references.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: To Obtain Explanation on Singleton Implementation

    I agree with you, but this is why I posted the wikipedia article (as much as credible it is)

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,512
    Blog Entries
    7
    Rep Power
    20

    Default Re: To Obtain Explanation on Singleton Implementation

    Quote Originally Posted by superhaNds View Post
    I agree with you, but this is why I posted the wikipedia article (as much as credible it is)
    Lets forget all about it; this morning I ran out of espresso so I turned a bit grumpy and philosophical; now that I had my espresso refill (I bought some this morning), I'm the happy old me again ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: To Obtain Explanation on Singleton Implementation

    I remember reading this about a year ago. It even covers multiple class loaders. I also believe a good way to declare a singleton is to use an enum (not certain if this article mentions that).

    http://www.javaworld.com/article/207...singleton.html

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,976
    Rep Power
    5

    Default Re: To Obtain Explanation on Singleton Implementation

    Quote Originally Posted by JosAH View Post
    I'm the happy old me again ;-)
    a grumpy happy old philosophical man on a caffeine (and possibly sugar) rush. World: beware.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,512
    Blog Entries
    7
    Rep Power
    20

    Default Re: To Obtain Explanation on Singleton Implementation

    Quote Originally Posted by gimbal2 View Post
    a grumpy happy old philosophical man on a caffeine (and possibly sugar) rush. World: beware.
    One is not supposed to take sugar with espresso; that's blasphemy, you rascal! One is not supposed to take sugar with Grolsch either ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,976
    Rep Power
    5

    Default Re: To Obtain Explanation on Singleton Implementation

    Quote Originally Posted by JosAH View Post
    One is not supposed to take sugar with espresso; that's blasphemy, you rascal! One is not supposed to take sugar with Grolsch either ...

    kind regards,

    Jos ;-)
    I'll accept the reprimand about the coffee, but I have to educate you about the beer: even though you don't ADD sugar to it, the beer itself is already mostly sugar (to your body).
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,512
    Blog Entries
    7
    Rep Power
    20

    Default Re: To Obtain Explanation on Singleton Implementation

    Quote Originally Posted by gimbal2 View Post
    I'll accept the reprimand about the coffee, but I have to educate you about the beer: even though you don't ADD sugar to it, the beer itself is already mostly sugar (to your body).

    Hogwash! I asked my liquor store manager and he told me that Grolsch is made from water, grains, a bit of bittering hops and nothing else; no sugar in my Grolsch, you atheist!

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: To Obtain Explanation on Singleton Implementation

    Quote Originally Posted by JosAH View Post
    Lets forget all about it; this morning I ran out of espresso so I turned a bit grumpy and philosophical; now that I had my espresso refill (I bought some this morning), I'm the happy old me again ;-)

    kind regards,

    Jos
    JUST Espresso? You really ARE getting old. ;-)

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,512
    Blog Entries
    7
    Rep Power
    20

    Default Re: To Obtain Explanation on Singleton Implementation

    Quote Originally Posted by masijade View Post
    JUST Espresso? You really ARE getting old. ;-)
    Yup, I only ran out of espresso; the supplies of Grolsch in my barn were taken care of by my liquor store manager; the bottle of Jenever was cooled in the fridge and a fresh packet of tobacco was in my vicinity ...

    thank you for your concerns and

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,976
    Rep Power
    5

    Default Re: To Obtain Explanation on Singleton Implementation

    Quote Originally Posted by JosAH View Post
    Hogwash! I asked my liquor store manager and he told me that Grolsch is made from water, grains, a bit of bittering hops and nothing else; no sugar in my Grolsch, you atheist!

    kindest regards,

    Jos
    Exactly! Grains! Now that might not be the sucrose you and your liquor store preacher mislabel as "sugar", to your body its still sugar anyway and it will have the same affect on your minus the rotting of teeth (IE - a sharp increase in blood sugar levels and the temporary inability of your body to process fat, thus storing it in your fat cells, optionally expanding them to make more room).

    I expected more from you. Now I feel even more alone in the world.

    Kindest Regards,

    Erik (<-- Atheist AND Paleo dork)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 1
    Last Post: 09-07-2011, 06:08 AM
  2. How to obtain the name of the JMenuItem?
    By Azuxard in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2009, 03:33 AM
  3. Replies: 2
    Last Post: 10-06-2008, 09:06 PM
  4. Replies: 0
    Last Post: 03-29-2008, 12:41 PM
  5. Replies: 0
    Last Post: 09-26-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •