javagenericsnested-generics

Understanding "improperly formed type, type arguments given on a raw type" when using typed inner class outside outer class


I am a little bit confused with the following and would be thankful for help.

What I wanted to do was implement a generic circular list where I can get access to the nodes (not their values) from outside. I will try to give a minimal setup which illustrates that.

class CircularList<T>{
    Node<T> first;
    /*Implementation details..., getters, setters */
    public Node<T> getFirst(){ return this.first };

    public class Node<U>{
        /*...*/
    }
}

Usage in another class:

CircularList<Vertex> vertices = new CircularList<>();
/*...*/
Node<Vertex> vertex = vertices.getFirst();

In the last line it says type arguments given on a raw type, which I don't really understand. I don't understand how this is a raw type. I thought through the type parameter of CircularList and the fact that getFirst returns a Node that the type wouldn't be raw but I obviously am mistaken.


Solution

  • You didn't specify the argument to CircularList:

    CircularList<Vertex>.Node<Vertex> vertex = vertices.getFirst();
    

    UPDATE

    But this doesn't make a lot of sense: you should either declare class Node<U> as static, or not specify any argument to it (and use argument T of the enclosing class).