Results 1 to 8 of 8
  1. #1
    MGT Madness is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Which is better?

    Java Code:
    private static ThisClass instance;
    
    public static ThisClass getInstance()
    {
            if (instance == null)
                    instance = new ThisClass();
            return instance;
    }
    Java Code:
    private static final ThisClass instance = new ThisClass();
    
    public final static ThisClass getInstance()
    {
            return instance;
    }


    ThisClass is the class name.
    Which one of these should i use to access a class? This is used to avoid static usage.
    They both produce the same results, not sure which is better.

    - Thank you :)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Which is better?

    The second one.
    The first one is not thread safe.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Which is better?

    Also the second one: why load a class if you don't want to use its singleton instance?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Which is better?

    Can't argue with that if this is all the information that is available. The first is lazy initialization and that is only really necessary when you are forced to delay the construction of the object until the last possible moment (ex: until other dependent classes/systems have initialized themselves for example), while the second example constructs the object at the first possible moment.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Which is better?

    The first is bad simply because it isn't thread safe.
    If you want lazy initialisation then use an inner class:
    Java Code:
    private class Lazy {
       private static final ThisClass instance = new ThisClass();
    }
    public final static ThisClass getInstance() {
       return Lazy.instance;
    }
    (Hope I got that right!)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Which is better?

    I'd rather just synchronize access - IF there is threading to speak of. Its not necessarily thread unsafe as long as the code is used within a single thread always.
    Last edited by gimbal2; 12-05-2013 at 11:46 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Which is better?

    Yes, but then in real life a singleton almost invariaby ends up in a multi threaded environment.

    And synchronizing on getInstance is single-threading the app at a (generally) bad point.
    Why narrow it there when you could simply lazy load (not that I've ever met a singleton that ever needed to do that).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Which is better?

    Lets just say that this comes dangerously close to discussions about the usage of design patterns and I have a very strong opinion about such matters not shared by all people, so lets just say that I agree with you in general because I don't want to go down that road of pointless discussions in yet another forum ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Posting Permissions

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