In Java, I know that you can do something like this:
public class Greeter {
public void greetEventually() {
final String greeting = "Hello!";
Job j = new Job() {
public void run() {
System.out.println(greeting);
}
};
j.schedule();
}
}
At some point in the future, this will lead to anonymous Job. This works because anonymous classes are allowed to refer to final variables in the scope.
I am not sure of the following:
public class Greeter {
private String greeting;
public void greetEventually() {
Job j = new Job() {
public void run() {
System.out.println(greeting);
}
};
j.schedule();
}
}
In this case, my anonymous one Jobrefers to the non-final field of the enclosing class. When the run is completed, will I see the value of the field greeting, as it was when creating the task, or how it happens when it is completed? I think I know the answer, but I thought it was an interesting question, and first he left me and a couple of colleagues guessing themselves for a few minutes.