wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Accessing a class' nested class from another class within the same package. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing a class Watch "Accessing a class New topic
Author

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

Juan Marcos
Greenhorn

Joined: Apr 05, 2011
Posts: 12

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

Joined: Apr 05, 2011
Posts: 12

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Apr 05, 2011
Posts: 12

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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Apr 05, 2011
Posts: 12

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!
 
 
subject: Accessing a class' nested class from another class within the same package.
 
Similar Threads
Generic class' constructor
Fail to create generic array?
Trees in Java
Question about TreeNode
cannot be applied to