Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: The type parameter E is hiding the type E when using an inner class

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.

like image 420
Vivin Avatar asked Aug 26 '14 16:08

Vivin


1 Answers

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 for Node would be one way to resolve the warning:

public class Stack<E> implements Iterable<E>
{
    private int N;
    private Node<E> first;

    private class Node<T>
    {
        private T item;
        private Node<T> next;
    }
}

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 and Stack<String>.Node are different types):

public class Stack<E> implements Iterable<E>
{
    private int N;
    private Node first;

    private class Node
    {
        private E item;
        private Node next;
    }
}

Note that this works because Node is not static, therefore it has a reference to the outer Stack<E> object, and because of this E must be defined. If Node is static then it has no real relationship to the outer Stack<E> type other than effectively being within its lexical scope.

like image 189
cdhowie Avatar answered Sep 29 '22 07:09

cdhowie