this.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
// How do I access the parent tree from here?
}
});
Asked
Active
Viewed 8,626 times
4

Christian Strempfer
- 7,291
- 6
- 50
- 75

Goutham
- 2,759
- 9
- 30
- 36
-
3What do you mean by parent tree? – Valentin Jacquemin Nov 05 '09 at 09:22
-
Ah sorry my bad. New to java and didn't realise TreeSelection was an interface. – Goutham Nov 05 '09 at 09:26
2 Answers
36
You can use OuterClass.this
:
public class Test {
String name; // Would normally be private of course!
public static void main(String[] args) throws Exception {
Test t = new Test();
t.name = "Jon";
t.foo();
}
public void foo() {
Runnable r = new Runnable() {
public void run() {
Test t = Test.this;
System.out.println(t.name);
}
};
r.run();
}
}
However, if you just need to access a member in the enclosing instance, rather than getting a reference to the instance itself, you can just access it directly:
Runnable r = new Runnable() {
public void run() {
System.out.println(name); // Access Test.this.name
}
};

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
-
1@Chris: In what way does it not answer the question? It absolutely shows how to get at the enclosing instance. At least, it answers the question I believe was asked. It's far from a clear question (the term "parent Object" isn't defined) but I assumed the OP meant "enclosing instance", in which case it answers the question. – Jon Skeet Sep 19 '12 at 07:16
-
Although Java itself doesn't define "parent", it's a common term for superclasses in object-oriented programming languages. A web search for "Java parent" or "Object-oriented parent" will show millions of hits. For a quick reference look at wikipedia for "class diagram", it says: "The superclass(base class) in the generalization relationship is also known as the "parent", superclass, base class, or base type." – Christian Strempfer Sep 19 '12 at 09:50
-
@Chris: Yes, but that's ignoring the context of an anonymous type, from the class... and the comment in the method, which talks about a *tree*, clearly distinct from the *listener*. I stand by my interpretation of the ambiguous question as being a valid interpretation. – Jon Skeet Sep 19 '12 at 12:38
-
I upvoted this because it answered my question which was how to access the outer class from within the inner class and I was directed to this page by Google. I accept what Chris is saying but I think that's a point of semantics. The fact is that this was useful to me, even if it technically didn't answer the asked question. – rbncrthms Jul 11 '14 at 14:00
3
TreeSelectionListener
is an interface, so the only parent class would be Object
, which you should be able to call with super
.
If you meant calling some method of the enclosing class, you can call it directly as within a method.

Christian Strempfer
- 7,291
- 6
- 50
- 75
-
Seems Like I was the one who downvoted the answer, because I did not get enough info to the posted Question: "How do I access the parent Object of an anonymous object in Java?". I did not understand why can I use super in the anonymous class... I thought you can't, because any call to `super` or `this` will correspond to the outer class, not the parent of anonymous... Hm... Sorry... I still don't know the correct answer, but seems like I was in a rush... Better would be to find the actual correct answer and then take a decision... – yashaka Sep 02 '13 at 06:06