As a C++ oldtimer I have managed to solve my problem but I can not wrap my head around the underlying Java mechanisms here:
Vector<Object> x = new Vector<Object>(); // OK
ArrayList<Object> y = new ArrayList<Object>(); // OK
List<Object> zzz = new ArrayList<Object>(); // OK solves problem below but question remains
List<Object> z = new List<Object>(); // WHY? Compiler error: Cannot instantiate
The List
is an interface. You cannot create in instance of an interface using new
operator. That's why the line List<Object> z = new List<Object>();
gives error. Only classes can be instantiated.
Yes. Because List is an Interface and in Java you cannot instantiate an Interface. You can only instantiate a class.
ArrayList is a class that's implementing List<> that's why you can instantiate it. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With