I was cleaning up code and changing all access to static member such that they are qualified by the class in which they are defined. This, however, lead to the following problem which is puzzling me.
I have a class with a nested class inside. In the annotation on this nested class I refer to a private static final field in the surrounding class. When doing this without qualification (as in the annotation on class D, below) this works. However, when adding the class qualifier (as in the annotation on class C) the compiler tells the field (v below) is not visible.
public class VisibilityTest {
@interface A {
int f();
}
@A(f = VisibilityTest.v) //fails
private static class C {
int c = VisibilityTest.v; //works
}
@A(f = v) //works
private static class D {
int d = VisibilityTest.v; //works
}
private final static int v = 5;
}
In both cases the variable refers to the same field, so why does this happen?
This compiles fine with 1.8.0_25 and 1.7.0_45 javac
, as it should really. Or both should fail, that would be consistent too.
This seems to be a bug in Eclipse's annotation handling (which is why you can happily reference the same constant from normal code), it was reported quite a long time ago but there hasn't beeen much activity over the last 4 years.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With