I was writing a stack, one with a static Node and other non-static.
public class Stack<E> implements Iterable<E>
{
private int N;
private Node<E> first;
private static class Node<E> // This works fine.
{
private E item;
private Node<E> next;
}
}
But when I try to make the Node non-static, it gives me this warning "The type parameter E is hiding the type E"
public class Stack<E> implements Iterable<E>
{
private int N;
private Node<E> first;
private class Node<E> // This shows warning.
{
private E item;
private Node<E> next;
}
}
A bit of understanding which I have tell me that since static memeber is a member of class so it does not give me a warning but when I make it non-static it belongs to the instance. But its not a clear thought.
It's warning you that you are using the generic argument name
E
in a scope when it already is defined. Changing the generic argument name forNode
would be one way to resolve the warning:But since E is already exists, you should just use that;
Node
is already generic due to being defined within a generic type (Stack<object>.Node
andStack<String>.Node
are different types):Note that this works because
Node
is not static, therefore it has a reference to the outerStack<E>
object, and because of thisE
must be defined. IfNode
is static then it has no real relationship to the outerStack<E>
type other than effectively being within its lexical scope.