• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Questions on innerclass and interface

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,
I have confusion on following codes.
1).

This code is giving error for InheritInner constructor saying 'no enclosing instance of type Outer is in scope'. Can somebody explain me why?
2).

Is any variable defined in an interface automatically become a final variable??
Please help me..
TIA.
Niral
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) You can't call a constructor for an inner class from a static method (such as main) unless you explicitly provide an instance of the outer class:
InheritInner ii = o.new InheritInner( )
2) All variables defined in an interface are public, static, and final.
 
Niral Trivedi
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron,
Actually InheritInner is not a nested class of Outer. It is just extending Outer.Inner. So, can I do that first of all?? I've tried though and got error.
Let me explain my situation here one more time..

If I don't do, o.new InheritInner()at line 4 then I am getting error 'no enclosing instance of type Outer is in schope' for line 1.
If I put o.new InheritInner() at line 4 then I am getting error 'Can not resolve Symbol class InheritInner' for line 4 which I think right as InheritInner is not an inner class of Outer.
Any idea??
Niral
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you extend an inner class, the default constructors are no longer created... or they no longer work. When you try to create a class of InheritInner, you are also trying to create the sub-object Inner (which is the super class). But since, Inner is an inner class, you need a refrence to the enclosing class object. This can be acheived by passing the reference of the Outer class to the constructor of InheritInner & explicitly invoking the constructor of Outer.
For the complete explanation, refer Bruce Eckel's Thinking in Java. Thats one hell of a book.

[ September 12, 2002: Message edited by: Vin Kris ]
 
Niral Trivedi
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ron..
Niral
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic