Results 1 to 11 of 11
  1. #1
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default Help me with this code

    class Singleton{
    public static Singleton Instance() {
    if (_instance == null)
    _instance = new Singleton();
    return _instance;
    }
    protected Singleton() {}
    private static Singleton _instance = null;

    }

    there is something wrong in above code..a big potential problem..i am not able to find it...

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What's wrong? You should know it, when you compile/run it'll give an error message. Did you read the message?

  3. #3
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default

    i compiled it there is no compilation problem...even code runs successfully...but the thing is that it is not good code to use..there is some potential problem...i am not able to find why this code is not usable...
    may be something related to mutithreading environment but m not able to find exact problem...

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    .but the thing is that it is not good code to use..there is some potential problem
    How do you know this?

  5. #5
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default

    actually it is an interview question...one of my friend had gone for interview..the technical person asked him this question.....

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by ron87 View Post
    actually it is an interview question...
    Ah! I thought you were asserting this to be true.

    Well, if your friend did not think to ask the technical person why the code was wrong (ie what the code was supposed to do, and under what circumstances it would not do that) then you're just stuck with Google. And the 50 million hits discussing the singleton pattern.

  7. #7
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default

    dude m not able find on google....tell me if u find the answer....

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    Java Code:
    class Singleton{
    public static Singleton Instance() {
     return new Singleton();
    }
    protected Singleton() {}
    }
    Singleton.Instance() should create new singleton object
    but the original code always return _instance
    as _instance is static, first call of Singelton.Instance() and second call of Singelton.Instance() reference to the same object(?) but not 2 differece Singleton object

  9. #9
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default

    @mtyoung

    SO what is exactly the problem...

  10. #10
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    as _instance is static, first call of Singelton.Instance() and second call of Singelton.Instance() reference to the same object(?) but not 2 differece Singleton object
    means no matter how many Singleton.Instance() call, they return the same object

    for example
    Java Code:
    Singleton a = Singleton.Instance();
    Singleton b = Singleton.Instance();
    modify a will modify b
    a and b are the identical object
    a, b point to the same position

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by ron87 View Post
    dude m not able find on google....tell me if u find the answer....
    The Google search engine may be more productive if you use full words. Dude.

    When I tried googling "+Singleton +Java" the first couple of hits were JavaWorld articles, including "When is a singleton not a singleton?" which was reprinted later by Sun. This article addresses the circumstances under which something intended to implement the Singleton pattern will, in fact, fail to do so.

    It does not address the question that many people raise: that the pattern itself is misused (ie is inappropriate in the circumstances in which it is used). And, if you're serious about investigating this question, you might like to explore their views.

Similar Threads

  1. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 02:39 PM
  2. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 11:12 PM
  3. Replies: 1
    Last Post: 04-26-2007, 04:52 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
  •