aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Questions on innerclass and interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Questions on innerclass and interface" Watch "Questions on innerclass and interface" New topic
Author

Questions on innerclass and interface

Niral Trivedi
Ranch Hand

Joined: Nov 26, 2001
Posts: 46
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

Joined: Jun 06, 2002
Posts: 1056
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)
Niral Trivedi
Ranch Hand

Joined: Nov 26, 2001
Posts: 46
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

Joined: Jun 17, 2002
Posts: 154
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

Joined: Nov 26, 2001
Posts: 46
Thanks Ron..
Niral
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questions on innerclass and interface