Results 1 to 8 of 8
- 04-24-2011, 05:48 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
Generic method invocation Clarification required
Hi,
I have a doubt regarding the generic method invocation described below.
This is a generic method declaration :
public static <E extends Number> List<? super E> process(List<E> nums){
return nums;
}
Now if i call the above method from the main method:
such as
ArrayList<Integer> n = null;
List<Number> numso= process(n);
The second line above in red gives a compilation error. Why does the "List<Number> numso" gives a compile error, since the return type declared is
"List<? super E>". If E is Integer then Super Of E can be Number and why List<Number> reference is not taking up the return type "List<? super E>" where E is an Integer ?
- 04-24-2011, 06:02 PM #2
You're trying to assign a List<E extends Number> object to a List<Number> reference. The types are not assignment-compatible.
To understand why, remember that you should be able to put any type of Number into a List<Number>. But a List<E extends Number> cannot hold any Number... it can only hold Es or subclasses of E.
- 04-24-2011, 06:30 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
A bit unclear in generic return type of a method
But the return type declared in the method is List<? super E>. If E is an Integer, then super of Integer can be a Number i.e at runtime the return type will be List<? super Integer>. So why cant List<Number> reference variable can accept the return type of List<? super Integer> from the method "public static <E extends Number> List<? super E> process(List<E> nums)" ?
- 04-24-2011, 07:00 PM #4
Hmmm... I misread your question. I'll be interested to learn the answer as well.
- 04-24-2011, 07:10 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
A bit more clarification required
Is it that during runtime/compiletime the method "public static <E extends Number> List<? super E> process(List<E> nums)" evaluates to "List<Integer>" instead of "List<? super Integer>" , when invoked like as
below:
ArrayList<Integer> n = null;
process(n);.
How does this happen can anyone please explain this ? Bit confused here.
- 04-24-2011, 07:24 PM #6
Maybe it's because if E is Integer, a method with a return type List<? super E> could return a List<Object> which would be incompatible with a List<Number> reference...
- 04-24-2011, 07:31 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
A bit reference needed
I think so it might be. But are there any rules written regarding this one, such as the return type declaration is meant only for the implementation within the method or is it for the reference variable. Does Java book for generics specifies this anywhere ? Any reference regarding this one.
- 04-24-2011, 10:59 PM #8
How about Sun's own tutorials? I would look into the Bounded Type Parameters section.
EDIT: Hmmm I just looked through it and it doesn't seem to explain returns with generics. Basically, You should change the return to List<Number> or List<E>.Last edited by ra4king; 04-24-2011 at 11:15 PM.
Similar Threads
-
RMI (remote method invocation) Java
By Stijn_vdd in forum NetworkingReplies: 1Last Post: 03-03-2011, 05:14 PM -
Clarification on main method
By Hype in forum Advanced JavaReplies: 5Last Post: 02-17-2011, 06:49 AM -
Generic method
By jomypgeorge in forum New To JavaReplies: 6Last Post: 01-18-2011, 05:51 AM -
HELP: Method Invocation
By rjuyal in forum Advanced JavaReplies: 4Last Post: 04-07-2008, 11:07 AM -
[/WEB-INF/applicationContext-dao.xml]: Invocation of init method
By Heather in forum JDBCReplies: 2Last Post: 06-12-2007, 04:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks