Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a private static fields be visible from nested class when qualified by the surrounding one?

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?

like image 784
miselico Avatar asked Oct 30 '22 09:10

miselico


1 Answers

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.

like image 153
biziclop Avatar answered Nov 09 '22 17:11

biziclop