For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2006 > A question about a generic method









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author A question about a generic method
Richard

2006-03-30, 7:04 pm

Hello,

The fill method in java.util.Collections has this signature:
<T> void fill(List<? super T> list, T obj)

I can't understand why the signature is not this one:
<T> void fill(List<T> list, T obj)
because with this last one you can call the method with these arguments
(Employee extends Person):
List<Person> l = new ArrayList<Person>();
l.add(new Person("john"));
l.add(new Person("bob"));
Employee e = new Employee("fred", 1000);
Coll.fill(l, e);

(T is inferred into Person).

I can't find circumstances where the first signature is better than the
last one. Could you show me one?

Thanks in advance for your answer.

Richard
Dave Mandelin

2006-03-30, 7:05 pm

I don't know either. I do know the type inference has more freedom with
the actual definition, because it can infer T to either Person or
Employee in your example, but I couldn't find a case where it actually
made a difference. Anyone know?

--
Need to get from a Foo object to a Bar object in Java?
Ask Prospector: http://snobol.cs.berkeley.edu
Want to play tabletop RPGs over the internet?
Check out Koboldsoft RPZen: http://www.koboldsoft.com

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com