Alex Rivera | Logout

"Instantiating" a List in Java?

Asked 2011-07-25T00:22:17.070
144

Trying to use the following code:

List<Integer> list = new List<Integer>();

I get the following error message:

java.util.List is abstract; cannot be instantiated

What does this mean and why can't I initialize a List the same way I would an ArrayList?

Edit
Report

1 Answer

4

A List isn't a real thing in Java. It's an interface, a way of defining how an object is allowed to interact with other objects. As such, it can't ever be instantiated. An ArrayList is an implementation of the List interface, as is a linked list, and so on. Use those instead.

answered 2011-07-25T00:24:37.327

Your Answer