0

Let's say a Tree class has in inner Node class.

class Tree {
    ...
    class Node {
        ...
    }
}

I know that within the context of a Node instance, the outer instance, or the "owner" of the Node can be accessed using the qualified this:

Tree.this.someAction();

I'm looking for the exact opposite of this: a way to refer to and iterate over all Nodes owned by a given Tree object without keeping an explicit collection field in Tree.

Is this even possible without reflection?

SMMH
  • 310
  • 1
  • 13
  • 1
    There is no out-of-the-box solution for this. You would have to write some bookkeeping-code yourself. – Turing85 Apr 25 '21 at 14:58
  • @Turing85 isn't this information already stored somewhere in the JVM? why not expose it to, at least, the owner class? – SMMH Apr 25 '21 at 16:03
  • 1
    To get this information, one would have to get (at least) all objects of that type and check their `parent`-reference. Sonds awfully inefficient. As to the "why is it not exposed": this is something you should ask James Gosling, @brian-goetz or on the [JCP](https://www.jcp.org/en/home/index). – Turing85 Apr 25 '21 at 16:10
  • @brian-goetz = Brian Goetz ([StackOverflow profile](https://stackoverflow.com/users/3553087/brian-goetz), [Twitter profille](https://twitter.com/BrianGoetz)) – Turing85 Apr 25 '21 at 16:21

0 Answers0