Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<? extends MyType>

I have a Java question about generics. I declared a generic list:

List<? extends MyType> listOfMyType; 

Then in some method I try instantiate and add items to that list:

listOfMyType = new ArrayList<MyType>(); listOfMyType.add(myTypeInstance);  

Where myTypeInstance is just an object of type MyType; it won't compile. It says:

The method add(capture#3-of ? extends MyType) in the type List<capture#3-of ? extends MyType> is not applicable for the arguments (MyType)

Any idea?

like image 886
Lancelot Avatar asked May 27 '09 21:05

Lancelot


1 Answers

You cannot do a "put" with extends . Look at Generics - Get and Put rule.

like image 55
Surya Avatar answered Sep 21 '22 10:09

Surya