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
Joined: Jun 06, 2002
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.
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Joined: Nov 26, 2001
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
Joined: Jun 17, 2002
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 ]