Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner classes

 
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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic