Hmmm... This is an interesting one.
I think the main problem here is outlined in
JLS 8.9 enums...
Nested enum types are implicitly static...
This implies that it is impossible to define a local (�14.3) enum, or to define an enum in an inner class (�8.1.3).
With respect to "this,"
JLS 15.8.3 this explains...
The keyword this may be used only in the body of an instance method, instance initializer or constructor, or in the initializer of an instance variable of a class. If it appears anywhere else, a compile-time error occurs.
When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (�15.12), or to the object being constructed.
Within a static context (not associated with any object), there is no "this."
I'm a little confused about the compiler's error messages regarding "this," but it's clear that these errors are somehow a consequence of trying to define the enum as an inner (anonymous) class, which is not allowed. If you move the anonymous class code outside of the static main method, then you get an
additional error message, "enum declarations allowed only in static contexts."
In any case, your third error is caused by simply trying to put too much in a single statement. The right side of the
assignment "MyClass m =" must return an instance of MyClass. But at the end of your instance creation, you are adding a call to ".display();" which returns
void, and this cannot be assigned to m.
Also note that the reference to the anonymous class instance is automatically upcast to type MyClass. So if display() is not declared in MyClass, you're not going to be able to reach it (or anything else that does not appear in the supertype, MyClass).