auto binding (type inference) of generic types by the compiler

后端 未结 5 662
梦毁少年i
梦毁少年i 2021-01-24 01:11

the following code has compilation error in the line of t3:

public  List getList()
{
    return new ArrayList();
}
public  vo         


        
5条回答
  •  既然无缘
    2021-01-24 01:47

    You are calling a generics method from generics method. You need to pass the generics argument from the first method to the getList method.

    List list = this.getList();
    

    and

    T t3 = this.getList().get(0);
    

    From these two the first compiles also without giving the generics argument because the compiler can get the type from the type of list (from the left side of the assignment). In the second case it isn't a direct assignment and so the compiler does not know the type for the getList() call.

    These may behave differently with different compilers.

提交回复
热议问题