-1

I'm working on an implementation of a double ended queue. I have a class Deque that has an inner class Node to represent the items on the list. I declare the class within Deque (itself a public class) like this:

public class Node(){
//
}

Now creating new nodes with basic object creation syntax is simple within the Deque class itself:

Node newNode = newNode(arg1);

However, I want to be able to declare new Nodes from a separate class, a DequeTest class that provides unit testing. When I attempt to create a new Node with the above syntax, I get an error saying that the Node class is not visible. I'm working on a pre-defined API, so I can't create any new public methods. Would a private createNode() method that returns a new Node be optimal? Even that seems like it wouldn't work, because the compiler throws an error when I even use the Node keyword.

user1427661
  • 11,158
  • 28
  • 90
  • 132

1 Answers1

0

Not possible. That's the whole point of making an inner class: it's only for the parent. Why would you want to? If you're using Node elsewhere it shouldn't be an inner class in the first place, since it doesn't belong to any one class. You might consider making a public Node class, then making separate inner classes subclass from Node.
See: here for API doc.

  • 3
    Have you read the article you link to? It's perfectly possible to obtain an instance of an inner class as long as it references an instance of the outer class. In other words, to create an instance of an inner class, you need an instance of its outer class. Also, the term *parent class* exists as a different name for *superclass*, it has nothing to do with outer classes. – toniedzwiedz Feb 18 '13 at 00:32