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

后端 未结 1 1303
星月不相逢
星月不相逢 2021-01-18 07:23

I was writing a stack, one with a static Node and other non-static.

public class Stack implements Iterable
{
    private int N;
    private         


        
1条回答
  •  醉话见心
    2021-01-18 07:29

    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 implements Iterable
    {
        private int N;
        private Node first;
    
        private class Node
        {
            private T item;
            private Node 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.Node and Stack.Node are different types):

    public class Stack implements Iterable
    {
        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 object, and because of this E must be defined. If Node is static then it has no real relationship to the outer Stack type other than effectively being within its lexical scope.

    0 讨论(0)
    提交回复
    热议问题