There's a very simple program:
public class A {
public static void main(String[] p) {
final Runnable r = new Runnable() {
public void run() {
System.out.println(r);
}
};
r.run();
}
}
And this gives:
$ javac A.java
A.java:6: variable r might not have been initialized
System.out.println(r);
^
1 error
- Why?
- How can a Runnable reference a variable pointing to it?
(In the real code, there is one more level (a listener), and referencing via this
does not work)
In this case, you can use "this" to avoid the compilation error:
To answer your second question:
should work.
Here the compiler sees that r will have been initialized through the implicit constructor.
For your tiny example, it would be enough to move the
final Runnable r
outside the main method and say:In this case your
new Runnable
is being created beforer
, because of order of operations everything to the right side of an assignment operator is performed before the assignment. This means thatr
is not in scope when your Runnable is declared, and thus it doesn't know what it is inside therun
method.just use "this" in the Runnable in the place of "r" ))) works fine. Then you do not need to add the "outer" Runnable.
This should work! And there is no r in the scope of run and thus it cannot be found in the anonymous implementation.
There's no
r
variable in the scope of yourrun()
method, you are trying to print out some value during it's initialization... That's why it"might not have been initialized"
.