• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JLS constructor example

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example below is from JLS 2.0 chap 8851
"If an anonymous class instance creation expression appears within an explicit constructor invocation statement (**), then the anonymous class may not refer to any of the enclosing instances of the class whose constructor is being invoked."
For example:

According to the explanation above the constr. being invoked is Dummy, so the enclosing instance of Dummy is class Toy...
Isn't the explanation in the JLS incomplete since I get an error if I declare int x in class Inside and Dummy also ???
Edited by Corey McGlone: Added CODE tags
[ February 25, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you'd like to say that the JLS is incomplete, that's fine, but I think you're really splitting hairs here. In fact, I imagine you can find the explanation for such a situation elsewhere in the JLS - perhaps just not in this example.
The problem we face in this situation is that you're trying to access an instance variable of an instance that doesn't yet exist. Obviously, that's a problem.
Whether that variable is part of Inside, Dummy, or Top doesn't really matter - at the point it is being used, none of those instances have been created.
If, however, you allow the constructor to complete, as in the following case, you no longer have a problem:

I hope that helps,
Corey
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic