List vs ArrayList

后端 未结 6 596
春和景丽
春和景丽 2021-01-23 20:31
List> listeners = new List>();

Why the line above fails with:

Cannot in

6条回答
  •  长发绾君心
    2021-01-23 20:48

    List cannot be instantiated, as it's just an interface.

    However, you potentially have another problem as well.

    Do you really have a class called 'E'? If you do, well, you shouldn't without a very good reason.

    Single letters such as E and T are pretty much exclusively used to denote a generic type parameter. Read it as: "This is a general description of how to make a class or method, without any reference to any specific type - you can parameterize this class by any legal reference type".

    So even classes like ArrayList<T> cannot be instantiated - because they are generic "recipes" for classes, not real concrete classes.

提交回复
热议问题