This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Classes" Watch "Inner Classes" New topic
Author

Inner Classes

Sharn Arora
Ranch Hand

Joined: May 09, 2006
Posts: 57
Following code gives compile time error on line 6
InheritInner.java:6: an enclosing instance that contains Outer.Inner is required.

Can anyone explain me the cause of the error and how to rectify it. Thanks.




SCJP 1.4 - 95%
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
class Outer {

class Inner { }
}

class InheritInner extends Outer.Inner {

InheritInner() {new Outer().super();}

public static void main(String[ ] args) {

Outer o = new Outer( );

InheritInner ii = new InheritInner( );
}
}

OR

class InheritInner extends Outer.Inner {

InheritInner(Outer o) {o.super();}

public static void main(String[ ] args) {

Outer o = new Outer();

InheritInner ii = new InheritInner(o);
}
}
Balaji VR
Ranch Hand

Joined: Mar 22, 2006
Posts: 76
Here we are trying to create a subclass for an Inner class. The complier obviously complains here because--->> When we try to create an object for "InheritInner", then it has to initialize "InheritInner" class AND all of its superclasses in the hierarchy!

Here in our case, the complier cant initialize a member (i.e the inner class "Inner" which is actually a part of another class i.e "Outer")

Hence the error!


Bala<br />SCJP 1.4 98%<br />SCBCD 1.3 -- 88%
Sharn Arora
Ranch Hand

Joined: May 09, 2006
Posts: 57
Thanks a lot Girish & Balaji
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Inner Classes