Declaring whether a class is generic or not affects other generic methods of the class (unlike generic parameters on this class)

the JDK8, code is as follows:

public class Foo<E> {

    public <T extends List> T get(T list) {
        return null;
    }

    public void pass(Foo<?> foo) {
        ArrayList arrayList = foo.get(new ArrayList());
    }

    public void broken(Foo foo) {
        //  Incompatible types,ArrayList
        ArrayList arrayList = foo.get(new ArrayList());
    }
}
The E and get methods of

Foo have different generic parameters T , so whether or not to declare Foo to be generic should not affect the get method.

is this intentional by the JAVA author for some reason, or is the compiler incapable of inference?

Sep.17,2021

this is the boundary problem of generics, and the java compiler does not allow unsafe type conversions.

the first method parameter defines Foo , telling the compiler that class generics are allowed to accept any type, so safe type conversions can be performed (actually accepting OBJECT)

the second method does not specify the generic type of Foo, so there is uncertainty about type conversion.

Menu