Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Capture of ?" Compile error, what does it mean?

 
Leonardo Crespo
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i'm playing with the code to better understand generics.

I tried to compile this:



Which to me is saying "put an ArrayList<Reptile> in a list of unknown. However, i'm getting this compile error:



I am particulary confused by the "capture of ? " message. Can anyone help me understand this error msg?

Thank you. Leonardo.
 
Prasanta Chinara
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the complete code always!

I hope you have the following code (where Reptile is a concrete class):



Here, the static type of the variable li is List<?>. You CAN NOT add anything to a "list of ?" because "?" indicates, we don't know what type it is.

Now the below code would work:


The reasoning is as below:

 
Prasanta Chinara
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No idea how that smily came in between ..
I meant:

((List<Reptile> li ) . add(new Reptile());
 
Leonardo Crespo
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it Prasanta, thank you for answering. I'll also make sure i always post the complete code.

Regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic