This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why l.add()  method is not working in this context? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "why l.add()  method is not working in this context?" Watch "why l.add()  method is not working in this context?" New topic
Author

why l.add() method is not working in this context?

sandeep sree
Greenhorn

Joined: Feb 17, 2008
Posts: 9
class p1
{
public static void main(String a[])
{
List<? extends String> l=new ArrayList<String>();
l.add("abc");

}
};
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14685
    
  16

"sandeep",
Welcome to the ranch. You may not be aware of the ranch Naming Policy. Please read it carefully and change your name accordingly (you need to set both first and last names). Thank you.


[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14685
    
  16

The parameter of add() is <? extends String> which means an unknown subtype of String is in there. String is not a great example. You should look at a simplier example like this :

So, l is a List<? extends A>. But what can you really put in there ? In the doSomething method, you don't know what kind of list it really is. Could you call l.add(new C()) ? C extends A. But we declared the list as containing B objects. With this type of wildcards, you can get objects from a collection, but not add any.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: why l.add() method is not working in this context?
 
Similar Threads
LinkedList problem
Problem While Removing Element From List
How can we search a name with a regex like "%" in a list of values
unmodifiableList(List list)
NullpointerException in Arrays.sort()