corey_trevor corey_trevor - 2 years ago 74
Java Question

Inner interface in generates errors only when outer class is parameterized? Why?

The abstract method getNextNode generates the error, "cannot make a static reference to the non-static type Node," but only if LinkedList is parameterized. If I remove the generic from LinkedList the error goes away. Why?

public class LinkedList<T> {
Node head;
public LinkedList() {
head = new Node();
private class Node {


interface stuff {
public Node getNextNode();//ERROR Cannot make a static reference to the non-static type Node

Answer Source

As the error is trying to tell you, you can't use a generic type without its parameter.

Node is actually LinkedList<T>.Node. Since your interface isn't generic (interfaces don't inherit type parameters from the containing class), there is no T it can substitute.

You can fix this by making the Node class static, so that it doesn't inherit the type parameter from its containing class.
However, you don't actually want to do that, since the Node should be generic.

You actually need to make your interface generic as well, so that it can specify a T.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download