6

I am trying to instantiate

LinkedList<?> op = new LinkedList<?>();

But I get error

Cannot instantiate the type LinkedList<?>

Why is it that this cannot be instantiated in Java?

1
  • 2
    What do you want to achieve? If you want a linked list that accepts any kind of object, new LinkedList<Object>() is the correct thing to do.
    – Heinzi
    Apr 6, 2013 at 12:17

1 Answer 1

6

If you don't know what object type will be inside the list, why are you using generics? <?> means I don't know (<? extends Object>).

It's been a long time since I programmed in Java but my understanding is that wildcard generics are made to match something, e.g. when you are declaring a method parameter, you can use a wildcard so that different generics can be passed to it. Also, a variable can use a wildcard to accept different generic types.

However, when creating an instance, you have to supply a specific type. The wildcard just doesn't have any sense there.

That's why

List<?> list = new LinkedList();
List<?> list = new LinkedList<Object>();
List<?> list = new LinkedList<MyObject>();

Are all possible but

List<?> list = new LinkedList<?>();

isn't.

5
  • 2
    Fun fact: starting with Java 7, you can write List<?> list = new LinkedList<>(); letting the compiler infer the type from the context, which is the target type List<?>. Whether it infers List<?> or List<Object> makes no difference to the outcome which is in no way more useful than writing List<?> list = new LinkedList<?>();. In the end, the actual answer is “because the specification says so”.
    – Holger
    Mar 16, 2016 at 12:24
  • I think @Holger is right here. To add to Holger's point, it's also possible to do this: List<List<?>> list = new ArrayList<List<?>>();. Why would the one be okay and not the other? I think Holger is right in saying that the actual answer is "because the specification says so." Sep 16, 2019 at 15:56
  • @johnnyodonnell a List<List<?>> is a bit different, as you can add actual lists to it. In fact, you can add any list to it as every List<Whatever> is assignable to List<?>. And a List<?> reference to an existing list can have practical use cases. However, the flexibility of a programming language allows a lot of constructs that are of not much use, but a language designer should think twice before deciding to add a special rule to forbid something, just because it’s deemed not useful…
    – Holger
    Sep 17, 2019 at 8:17
  • @Holger I would argue that List<?> list = new ArrayList<?>() would have just as many practical use cases as List<List<?>> list = new ArrayList<List<?>>(); however, I've never actually needed either of these constructs in practice. Sep 17, 2019 at 14:07
  • None of those examples will work. When you use List<?> as the static type you are essentially saying a list of no known type. It may compile but the only value permissible for storage in that List would be null. Putting any other object type in there would not compile.
    – Andrew S
    Mar 22, 2021 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.