1

I have the following (partial) class:

public class Graph<O> {
    private ArrayList<Edge> edges;

    public ArrayList<Edge> getEdges() {
        return edges;
    }
}

Now, when calling the method getEdges() somewhere else and storing the result in a variable of type ArrayList<Edge>, I get warning: [unchecked] unchecked conversion:

OtherFile.java:101: warning: [unchecked] unchecked conversion
        ArrayList<Edge> edges = graph.getEdges();
                                              ^
  required: ArrayList<Edge>
  found:    ArrayList

I have looked at multiple other questions about this warning, but I can't figure out what I'm doing wrong. getEdges() returns ArrayList<Edge>, so why can't I store its result in a variable of that exact type?

1 Answer 1

5

That warning would come when you invoke the getEdges() method on raw type Graph. When that is the case, all the generic types are replaced with their erasure. So, for Graph raw type, the method signature becomes like:

public ArrayList getEdges();

Solution is to use parameterized type or wildcard types.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I changed the definition of graph in the other file (class) from Graph to Graph<?> and now it compiles without errors indeed :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.