I have a singleton class that is returning multiple instances, and I can't figure out how. It is likely that there are multiple threads calling on the singleton class, but if that is the case, I'm not sure how I can make it work properly. Here's some of the code.
public class SomeClass{
private static final String TAG="someClass";
private volatile static SomeClass instance = new SomeClass();
public static synchronized SomeClass getInstance() {
Log.v(TAG,"Returning instance "+instance);
return instance;
}
private SomeClass() {
//Some initialization here
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append("this="+System.identityHashCode(this));
return sb.toString();
}
}
And my logs look like this:
11-02 12:50:04.494: V/someClass(12882): Returning instance this=1386326216
11-02 12:50:04.518: V/someClass(12900): Returning instance this=1384153464
I don't understand how I can have 2 instances, given how I'm setting this up. Any ideas?