How to get a general holder class that contains an object of any type. In your case, it may contain a Boolean type. Something like:
class Holder<T> { private T genericObj; public Holder(T genericObj) { this.genericObj = genericObj; } public T getGenericObj() { return genericObj; } public void setGenericObj(T genericObj) { this.genericObj = genericObj; } }
And use it like:
public class Test { public static void main(String[] args) throws Exception { final Holder<Boolean> boolHolder = new Holder<Boolean>(Boolean.TRUE); new Runnable() { @Override public void run() { boolHolder.setGenericObj(Boolean.FALSE); } }; } }
Of course, this has the usual problems arising with mutable objects that are thread-separated, but you get the idea. Plus, for applications where memory requirements are strict, this can be tailored when performing optimization if you have many calls to such methods. In addition, using AtomicReference to exchange / set links should take care of using from multiple threads, although using it across threads will still be a bit dubious.
Sanjay T. Sharma
source share