private static Callback callback;
public Foo()
{
super(getCallback());
}
private static Callback getCallback()
{
callback = new Callback();
return callback;
}
Constructor Foo() can potentially be called from multiple threads. My concern is with the private static field 'callback' and the static method 'getCallback()'.
As can be seen, every time 'getCallback()' is called, it assigns a new value to static field 'callback'.
My guess is that it is not thread safe because the keyword static is always attached to the class not the instance, so that means, the static field 'callback' of a Foo can potentially be overwritten by other thread that is constructing another Foo(). Is this correct?
Please correct me if I am wrong. Thanks!
EDIT: My intention is to keep 'callback' somewhere in the class, so I can reuse it later on. But this is not easy because Foo extends from a class that has constructor mandating 'callback' to be passed on.