• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Juan Marcos
Greenhorn
Posts: 12
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Juan Marcos
Greenhorn
Posts: 12
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Clapham
Sheriff
Posts: 20998
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't explain why your first try "didn't work" so it's pretty hard to suggest how to fix that.
 
Juan Marcos
Greenhorn
Posts: 12
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Juan Marcos
Greenhorn
Posts: 12
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic