I was trying to declare an enum inside an anonymous class with in a static context. I know that I'm trying out some thing freaky here and also I had this error in place.But I didn't understand what the error meant.
On compiling the error is
My question is what is this in the above error referring to. Is that referrring to enum.
My question may look very dumb, but very curious to know about the outcome of my freaky code
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).
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org