3 Replies Latest reply on May 11, 2012 4:38 PM by 936584

    Generic Methods


      I've created a class with a generic method as follows:
      public class Box<T> {
           public static <U> void someGenericMethod(U u, U v) {
                System.out.println("u: " + u.getClass().getName());
                System.out.println("v: " + v.getClass().getName());
      And then wrote some code to test it:
      Box.someGenericMethos(new Integer(10), "some text");
      This works perfectly. The compiler infers the type for each parameter u and v, one is an
      Integer and the other one is a String.

      I modified the generic method as follows:
           public static <U> void someGenericMethos(U u, List<Box2<U>> boxes) {
                System.out.println("u: " + u.getClass().getName());
                System.out.println("v: " + boxes.getClass().getName());
      And wrote some code to test it again:
                List<Box2<String>> boxes = new ArrayList<Box2<String>>();
                Box2.someGenericMethos(new Integer(10), boxes); // Compiler error
      Now I have a compiler error.

      My question is, Why the compiler doesn't infer the type for both arguments as in the first case?

      Thanks in advance.

      Edited by: EJP on 11/05/2012 09:34: added {noformat}
      {noformat} tags: please use them. Code is basically illegible without them.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        • 1. Re: Generic Methods
          Now I have a compiler error.
          What compile error?

          Posting statements like that is a complete waste of time.
          • 2. Re: Generic Methods
            The problem is that since your are using an Integer and String, the only possible value for the type U is Object. that means the method is expecting a "List<Box2<Object>>", but you are passing it a "List<Box2<String>>", which is not the same type. nested generics are a little trickier. generally, the solution to such situations is to add (one or more) "? extends " to the nested type in your method parameter. i believe that in your case, you will need "List<? extends Box2<? extends U>>".
            1 person found this helpful
            • 3. Re: Generic Methods

              First of all thanks a lot for your answers.

              I'm sorry I didn't say what the compile error was, it won't happen again.

              This is the message error:
              The parameterized method <Integer>addBox(Integer, List<Box<Integer>>) of the type Box is not applicable for the arguments (Integer, List<Box<String>>)

              But now I understand why I'm getting this error, or at least that's what I think.

              The solution is a little bit harder to understand right now, but I'm gonna study and think about it.

              Thanks again for your answers.

              PD: Where can I find all the tags I can use to write my posts?