2

If you have a Java Singleton that looks like this:

public class MySingleton {

private static MySingleton instance;

private int member;

public static MySingleton getInstance(){
    if(instance==null){
        instance = new MySingleton();
    }
    return instance;
}

private MySingleton(){
    //empty private constructor
}

public int getMemberA(){
    return member;
}

public int getMemberB(){
    return instance.member;
}

}

...is there a difference between getMemberA and getMemberB? That is, is there a difference between accessing the member with instance.xxx and just xxx?

Note: I am aware of the pros and cons of using the Singleton pattern!

hooby3dfx
  • 936
  • 1
  • 8
  • 18

4 Answers4

11

Yes, there's a difference.

Your singleton implementation isn't currently threadsafe, which means it's possible to call getMemberB() on an instance other than the one referred to by instance, at which point you'll get a different result.

If your implementation were thread-safe (so genuinely only one instance could ever be created) then they'd be equivalent, and the simpler form would be much preferred.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • What is your preferred approach? [This seems nice to me.](http://en.wikipedia.org/wiki/Singleton_pattern#The_solution_of_Bill_Pugh) – hooby3dfx May 17 '12 at 15:36
  • @hooby3dfx: It depends on your requirements. The nested class approach is more hassle than it's worth unless you really need ultra-lazy instantiation and other static methods. The enum approach has its benefits. Personally I try to avoid the singleton pattern in the first place, so it rarely comes up for me... – Jon Skeet May 17 '12 at 15:49
4

No functional difference, but I find getMemberA() easier on the eye.

Note that your singleton isn't thread-safe. Two threads calling getInstance() concurrently could result in the creation of two objects. If this happens, the singleton contract is broken, and all bets are off.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

No difference in behavior, however, I'd rather use 'return member' or even 'return this.member' as this looks more intuitively.

Thread-safety is a completely different topic and this simple singleton does not meet any thread-safe singleton requirements.

Jacek M
  • 2,349
  • 5
  • 22
  • 47
0

Your implementation of Singleton pattern uses lazy load technique. But it is not thread safe. We can use the synchronized keyword to make getInstance() method thread safe, but it hurts performance. It's better that we make double-check on private singleton member.

public class MySingleton {

    private volatile static MySingleton instance;

    public static MySingleton getInstance(){
        if (instance == null) {
            synchronized (MySingleton.class) {
                if (instance == null) {
                    instance = new MySingleton();
                }
            }
        }
        return instance;
    }

    private MySingleton(){
        //empty private constructor
    }
}
The Tran
  • 400
  • 4
  • 6