• 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

not an enclosing class

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to create an instance of an inner class that's within an inner class and javac gives a compile error of
not an enclosing class
Here's my code:

jikes gives me this error:
*** Error: An instance of "XY$X.this" is not accessible here because it would have to cross a static region in the intervening type "XY".
I'm not sure why the language won't allow me to create an instance of class Y.
[This message has been edited by Jim Yingst (edited July 27, 2001).]
 
Kai Middleton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to refine my example somewhat. I think the error message I got from jikes is somewhat misleading so I used a non-static scenario:

jikes then reports this:

*** Error: An instance of "XY$X.this" is not accessible here. In general, an enclosing instance is accessible only in the body of an instance method, constructor (after the explicit constructor invocation, if any), initializer block, or in the initializer expression of an instance variable.


Again, what I would like to do is create an instance of my inner-inner class within my main class.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[This is a response to the first post - I only saw the second after I wrote this.]
Because Y is a nonstatic member of X, you need to create an X instance before you can create a Y instance. And since X is a nonstatic member of XY, you need an instance of XY before you can create an instance of X. (Remember, you're starting from main(), which is static and has no instances of anything to begin with.) So, replace
<code><pre>
X.Y xy = new X.Y();
</pre></code>
with
<code><pre>
XY xy = new XY();
X x = xy.new X();
X.Y y = x.new Y();
</pre></code>
or more compactly
<code><pre>
X.Y y = new XY().new X().new Y();
</pre></code>
Note that since your original variable "xy" was really a Y instance, I renamed it to "y" to avoid confusing it with the real class XY, whose instance was more deserving of the name "xy".
I assume this is just a learning example. From a class design perspective, it's pretty atrocious of course - if you want to be able to create instances of a class from a static context (the main method), you probably shouldn't declare that class to be a nonstatic member of another class. In fact nonstatic member classes are of limited use for precisely this reason - usually a static member class would be preferable, unless you find you need access to an enclosing instance, which you get with a nonstatic member class.
[This message has been edited by Jim Yingst (edited July 27, 2001).]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, on reading your second post, you're halfway there - you still need to create an instance of X at some point, before you can create a Y.
In retrospect, wouldn't class names like X, Y, Z have been clearer than XY, X, Y? Or Outer, Middle, Inner would have been even clearer. Ah well...
 
Kai Middleton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. I've refined my example to show class names a little more clearly and also (hopefully) to show my motivation for this example. Basically I want to create an instance of the Deep inner class. I guess people would argue I'm not respecting encapsulation with what I'm trying to do, but my idea is that the SomeClass class should be able to have some simple inner classes that it can manipulate at will. I'm not sure what the philosophies here are or "should" be, but I thought what I was trying to do would be straightforward. Perhaps someone could comment on that above and beyond the java syntax.


javac gives this error:

SomeClass.java:11: not an enclosing class: SomeClass.Inside
Inside.Deep id = new Inside.Deep(); // PROBLEM HERE


jikes gives this error:

Found 1 semantic error compiling "SomeClass.java":

11. Inside.Deep id = new Inside.Deep(); // PROBLEM HERE

--

*** Error: An instance of "SomeClass$Inside.this" is not accessible here. In general, an enclosing instance is accessible only in the body of an instance method, constructor (after the explicit constructor invocation, if any), initializer block, or in the initializer expression of an instance variable.

------------------
java, database and HTML programmer
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic