I just answered this question by saying how to solve the compilation problem:
How to use fields in java enum by overriding the method?
But what I don\'t unde
Class FIRST
is an inner class of MyClass
and also a sub class. The reason you do not see an error when accessing someField
in it is because you are accessing the someField
of the outer class, not the super class.
class MyClass {
class FIRST extends MyClass {
@Override
public String doIt() {
super.someField = "super";
return "1: " + someField;
}
};
private String someField = "outer";
public String doIt(){return "";}
public static void main(String[] args) {
System.out.println(new MyClass().new FIRST().doIt());
}
}
Prints 1: outer
.
In the other case your enum constants behave as static nested sub classes, not inner classes, so they do not have a reference to the outer class, only their super class.
When an identifier is resolved, Java prefers the lexical scope over inherited members. So when you have an inner class that extends the outer class and use a field of the outer class without using this
or super
, the field of the outer instance is accessed which fails if the inner class is static
as there is no outer instance then. In contrast, when using super
you are explicitly accessing the inherited member. Note that enum
classes are implicitly static
. You can even use this
to access the inherited member but you have to use ((MyClass)this).someField
to access it if it’s declared private
.
I disagree with the accepted answer.
The enum const declaration is implicit public static final
, but not the class the enum const belongs to.
From JSL Chapter 8.Classes
The optional class body of an enum constant implicitly defines an anonymous class declaration (§15.9.5) that extends the immediately enclosing enum type. The class body is governed by the usual rules of anonymous classes.
And what the 'rules of anonmous classes'?
From JSL Chapter 15:
An anonymous class declaration is automatically derived from a class instance creation expression by the Java compiler.
An anonymous class is never abstract (§8.1.1.1).
An anonymous class is always implicitly final (§8.1.1.2).
An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).
And if the enum equivalent class is a static class, how to explain the following error?
public enum MyClass {
First {
public static int b; //(2)Illegal static declaration in inner class
};
}
But why a inner class can't access the outer class's field?
A possible enum equivalent class may looks like following, which gives the same error as a enum class:
abstract class MyClass {
private int someField;
static {
class First extends MyClass {
public void method() {
System.out.println(someField);
}
private static int b;
}
}
}
More:
Your abstract class is not equivalent to your enum, since enums are implicitly public static final. Thus, you'll observe the same behavior if you use:
abstract class MyClass {
static class FIRST extends MyClass {
@Override
public String doIt() {
return "1: " + someField; // error
}
};
static class SECOND extends MyClass {
@Override
public String doIt() {
return "2: " + super.someField; // no error
}
};
private String someField;
public abstract String doIt();
}
As explained in http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html, chapter "Static Nested Classes":
A static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.
Thus the need of super
. You could also use this
if the field were protected
rather than private
.