• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Enums in ananymous InnerClasses

Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
My cellmate was this tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic