Results 1 to 8 of 8
  1. #1
    muzu1232005 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Unhappy 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 ?

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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.

  3. #3
    muzu1232005 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Red face 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)" ?

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Hmmm... I misread your question. I'll be interested to learn the answer as well.

  5. #5
    muzu1232005 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Red face 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.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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...

  7. #7
    muzu1232005 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default 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.

  8. #8
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    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

  1. RMI (remote method invocation) Java
    By Stijn_vdd in forum Networking
    Replies: 1
    Last Post: 03-03-2011, 05:14 PM
  2. Clarification on main method
    By Hype in forum Advanced Java
    Replies: 5
    Last Post: 02-17-2011, 06:49 AM
  3. Generic method
    By jomypgeorge in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 05:51 AM
  4. HELP: Method Invocation
    By rjuyal in forum Advanced Java
    Replies: 4
    Last Post: 04-07-2008, 11:07 AM
  5. Replies: 2
    Last Post: 06-12-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •