Here is the program
public class Closure {
public String foo = "";
public static Closure process(final Closure t) {
System.out.println(t.toString() + " = " + t.foo);
t.foo = "bar";
new Runnable() {
public void run() {
System.out.println(t.toString() + " = " + t.foo);
t.foo = "baz";
}
}.run();
System.out.println(t.toString() + " = " + t.foo);
return t;
}
public static void main(String[] args) {
process(new Closure());
}
}
When I execute it, all 3 prints show the same reference for the variable t.foo. This makes sense for Closures, it is as we expect it.
What puzzles me is that in Java Strings are immutable. So if we reassign the string, shouldn't the reference change?