File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner  classes" Watch "Inner  classes" New topic

Inner classes

Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
class OClass {
public OClass() {
IClass i=new IClass();
public class IClass {
private int a;
class ISubclass extends OClass.IClass{
Why the hell doesn't this code compile?
alex earnshaw
Ranch Hand

Joined: Nov 05, 2001
Posts: 60
Originally posted by Gautam Sewani:

Why the hell doesn't this code compile?

Not sure if this is the reason but try making the outer class 'public'. In this instance you have OClass which has default access and its inner class IClass which is public. I think OClass needs to be public too...
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

problem # 1:
In a file named, the outer class OClass needs to be defined as public.
problem #2:
Extending the inner class IClass:

receives the error:
"no enclosing instance of type com.javaranch.OClass is in scope"
Because IClass is a non-static innerclass, it has the Enclosing class as part of its context. So, when extending this class, you also need to provide it an instance of the enclosing class, like this:

problem #3:
There's no main method -- so nothing's going to happen when you run it.

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Arun Pai
Ranch Hand

Joined: Mar 11, 2002
Posts: 143
javacertstudy in Yahoo groups.
I agree. Here's the link:
subject: Inner classes
It's not a secret anymore!