Consider the following code:
enum E {
A { public int get() { return i; } },
B { public int get() { return this.i; } },
C { public int get() { return super.i; } },
D { public int get() { return D.i; } };
private int i = 0;
E() { this.i = 1; }
public abstract int get();
}
I get compile-time errors on the first 2 enum constants declarations (A & B) but the last 2 compile fine (C & D). The errors are:
Error 1 on line A: non-static variable i cannot be referenced from a static context
Error 2 on line B: i has private access in E
Since get is an instance method, I don’t understand why I can’t access the instance variable i the way I want.
Note: removing the private keyword from the declaration of i also makes the code compilable, which I don’t understand either.
Using Oracle JDK 7u9.
EDIT
As pointed out in the comments, this is not specific to enums and the code below produces the same behaviour:
class E {
static E a = new E() { public int get() { return i; } };
static E b = new E() { public int get() { return this.i; } };
static E c = new E() { public int get() { return super.i; } };
static E d = new E() { public int get() { return d.i; } };
private int i = 0;
}
The observed behavior is mandated by the Java Language Specification, in particular implicit access to fields of enclosing types, and the rule that private members are not inherited.
unqualified field access
The spec mandates:
This makes the expression
isomewhat ambiguous: Are we referring to the field of the enclosing instance, or the inner instance? Alas, the inner instance doesn’t inherit the field:Therefore, the compiler concludes we mean to access the field of the enclosing instance – but being in a static block, there is no enclosing instance, hence the error.
field access through
thisThe spec mandates:
Therefore, it is quite clear we want the field of the inner class, not the outer one.
The reason the compiler rejects the field access expression
this.iis:That is, a private field can only be accessed through a reference of the type that declares the field, not a subtype thereof. Indeed,
compiles just fine.
access through super
Like this, super refers to the object the method was invoked on (or the object being constructed). It is therefore clear we mean the inner instance.
Additionally, super is of type
E, and the declaration therefore visible.access through other field
Here,
Dis an unqualified access to the static fieldDdeclared inE. As it is a static field, the question of which instance to use is moot, and the access valid.It is however quite brittle, as the field is only assigned once the enum object is fully constructed. Should somebody invoke get() during construction, a
NullPointerExceptionwould be thrown.Recommendation
As we have seen, accessing private fields of other types is subject to somewhat complex restrictions. As it is rarely needed, developers may be unaware of these subtleties.
While making the field
protectedwould weaken access control (i.e. permit other classes in the package to access the field), it would avoid these issues.