Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions on innerclass and interface

 
Niral Trivedi
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Ron..
Niral
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic