• 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

Inner Classes

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this small code
class OuterTrial{
int y = 9;
class InnerTrial{
int hello = 8;
}
static public void main(String[] Args){
OuterTrial outer = new OuterTrial();
OuterTrial.InnerTrial inner = outer.new InnerTrial();
}
}
Everything works just fine. No compilation error.
But if I change the last line to
outerTrial.InnerTrial inner = new InnerTrial();
it gives me this compilation error:
OuterTrial.java:15: No enclosing instance of class OuterTrial is in scope; an explicit one must be provided when creating inner class OuterTrial. InnerTrial, as in "outer. new Inner()" or "outer. super()".
OuterTrial.InnerTrial inner = new InnerTrial();
^
i understand outer.new Inner() and it works fine when i write
"OuterTrial.InnerTrial inner = outer.new InnerTrial();" in the code above.
but I don't know how to use outer.super();
it doesn't work when i use
"OuterTrial.InnerTrial inner = outer.super();"
I also don't understand why should someone use super() with the outer Object.
Thanks alot.
Purvi

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Outside of the inner class definition, invocation of an inner class method or constructor must always occur through an instance of the outer class. Although its not applicable to your code, another way to get an instance of the outer class could, in some situations, be with an explicit ctor invocation using <code>super()</code>.
The compiler is just trying to cover multiple cases with a single error message; the part about <code>super</code> doesn't apply in your case.
I get a different error with v1.3, so I guess you're using an earlier version. I note that the offending line in your post is line 9, not 15 as your compiler message shows; maybe the 6 lines you edited out have something to do with it. Have you also edited the compiler message? I noticed some spaces (after "<code>OuterTrial.</code> and "<code>outer.</code>") that have me wondering.
 
purvi shah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yes, I edited some lines in my source code. Those were some System.out.println statements.
But i have not edited anything from the Compilation error part.
I too noticed those spaces, but since Java ignores spaces, I thought its not so important. I am curious as to why did you notice it? Does that make any difference?
I m using jdk1.2.2.
Can u also tell me, in what conditions you would use "super()", and how would you use it.
Thanking You for ur reply.
Sincerely,
Purvi
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic