Cannot instantiate the type List<Product>

Emanuel picture Emanuel · Oct 31, 2011 · Viewed 153k times · Source

I have the following code:

List<Product> product = new List<Product>();

The error:

Cannot instantiate the type List<Product>

Product is an Entity in my EJB project. Why I'm getting this error?

Answer

Matt Ball picture Matt Ball · Oct 31, 2011

List is an interface. Interfaces cannot be instantiated. Only concrete types can be instantiated. You probably want to use an ArrayList, which is an implementation of the List interface.

List<Product> products = new ArrayList<Product>();