• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner classes

 
Gautam Sewani
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class OClass {
public OClass() {
IClass i=new IClass();
System.out.println(i.a);
}
public class IClass {
private int a;
}
}
class ISubclass extends OClass.IClass{
}
Why the hell doesn't this code compile?
 
alex earnshaw
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problem # 1:
In a file named OClass.java, 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.
 
Arun Pai
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautam,
javacertstudy in Yahoo groups.
-Arun
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic