Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner classes  RSS feed

 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one please prrove with small examples that,
non-static inner classes have reference of outer class reference assosiated with them while static inner classes and local inner
classes does not have.
- Thanks
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But local classes do have outer instances associated with them, as long as the local class isn't defined in a static context:
<code><pre>
class Outer {

private String field = "field which is part of an Outer instance";

public void method() {
class Local {
public void message() {
System.out.println("See, I can access a " + Outer.this.field);
}
}
new Local().message();
}

public static void main(String[] args) {
new Outer().method();
}
}
</pre></code>
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!