Is there a better reference container than a one element array?

A Java anonymous class can only access a variable from the surrounding method if that variable is final, and hence the inner class can't write to that variable. In order to capture an object created by an enclosed type, I've created a final single-element array to act as a reference:

    final String[] ref = new String[1];
    final Runnable runnable = new Runnable() {
        public void run() {
            ref[0] = "Hello world";
        }
    };

    runnable.run();
    System.out.println(ref[0]);

This appears to work, and I imagine has good performance. But it feels slightly hacky.

Obviously it is trivial to write a Ref class to replace this such that:

    final Ref<String> ref = new Ref<>();
    final Runnable runnable = new Runnable() {
        public void run() {
            ref.set("Hello world");
        }
    };

    runnable.run();
    System.out.println(ref.get());

... is there an existing class in the standard Java runtime that does this?

I've looked at java.lang.ref.Reference, and this isn't the same thing. These are immutable, the only implementations are for weak/phantom references.

Jon Skeet
people
quotationmark

There's AtomicReference<T> which is what I'd normally use.

final AtomicReference<String> ref = new AtomicReference<String>();
final Runnable runnable = new Runnable() {
    public void run() {
        ref.set("Hello world");
    }
};

runnable.run();
System.out.println(ref.get());

The only downside here is that unless multiple threads are involved, it's slightly misleading as AtomicReference is usually designed for concurrent access - but as a simple wrapper object, I think it's a nice solution.

people

See more on this question at Stackoverflow