Forums Register Login

Accessing a class' nested class from another class within the same package.

+Pie Number of slices to send: Send
Howdy!

Wondering if someone can help.

I have two classes within the same package.

and...

The countNodes() method in the BinaryTreeNodeCount class uses the nested TreeNode class within
the TreeNodePlay class. I don't know how to acces TreeNode from TreeNodePlay. I've tried importing
import TreeNodePlay.TreeNode;
Yet that didn't work.

Any recommendations? Thanks!
+Pie Number of slices to send: Send
Hmm... what worked so far was that I made the nested class TreeNode static.



I'm thinking that there must be a more efficient way. Any thoughts?
+Pie Number of slices to send: Send
You didn't explain why your first try "didn't work" so it's pretty hard to suggest how to fix that.
+Pie Number of slices to send: Send
Hi Paul,

Earlier, I wasn't able to access the nested TreeNode class within the TreeNodePlay class.
So I made it static, and I just accessed it through a static call, i.e.,
TreeNodePlay.TreeNode

I've gained access to it now, so I'm just trying to see if there's a more efficient way of
accessing the nested class (please see my second post).

I'm not getting any errors, and the class is accessible to me. Just want to know if there's
a more efficient way to access the TreeNode class within the TreeNodePlay class.

I'm gonna go through the oracle sight to see if I can find a way there as well.

Thanks!
+Pie Number of slices to send: Send
Instances of (non-static) inner classes only exist in the context of an instance of the enclosing class. So before you made it static, you needed a TreeNodePlay object before you could create a TreeNode.

That doesn't seem to match what you wanted, so I think what you're doing now is the right approach - make it static and access it using the full TreeNodePlay.TreeNode notiation. You could use a static import to avoid having to specify TreeNodePlay, but to be honest I wouldn't do that as I think it makes it less clear.

That is, of course, if it makes sense for it to be an inner class at all. It's difficult to be sure without knowing how the rest of the application fits together, but from the look if it I might just have TreeNode as a top-level class.
+Pie Number of slices to send: Send
 

Instances of (non-static) inner classes only exist in the context of an instance of the enclosing class. So before you made it static, you needed a TreeNodePlay object before you could create a TreeNode.


Thanks Matthew.

That makes complete sense. Honestly, I think the best pragmatic way to go about it would
be to just create a concrete class "TreeNode" on it's own. But I just wanted to get a grip
on accessing it from it's nested form, if I ever needed to do that.

Except for education, my app doesn't really serve a purpose. Hehe, I was actually in the middle of
learning about Linked Data Structures and Recursive Algorithms. But as i always do when I come
up on something that catches my curiosity with Java, I digressed.

Thanks again!
Whatever. Here's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 633 times.
Similar Threads
cannot be applied to
Trees in Java
Generic class' constructor
Fail to create generic array?
Question about TreeNode
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:03:20.