http://docs.oracle.com/javase/tutorial/java/generics/genmethods.html
Quoting from there-
A more realistic use of generic methods might be something like the following, which defines a static method that stuffs references to a single item into multiple boxes:
public static <U> void fillBoxes(U u, List<Box<U>> boxes) { for (Box<U> box: boxes { box.add(u); }}
Here, what does this List<Box<U>> do? How does this work?
Further,
To use this method, your code would look something like the following:
Crayon red = ...; List<Box<Crayon>> crayonBoxes = ...; The complete syntax for invoking this method is: Box.<Crayon>fillBoxes(red, crayonBoxes);
I couldn’t understand all these.
List is a generic collection.
Box is another generic.
You can declare
Box<Crayon>to construct a Box of CrayonsList<Box>is a collection of Boxes but Box is generic, need a Type for the boxList<Box<Crayon>>.Just follow the generic type U. FillBoxes method is static
public static <U> void fillBoxes(U u, List<Box<U>> boxes). Static methods doesn’t need a instance, if there is not a instance you can’t declare the type of the boxBox<Crayon>, you have to pass the type in the call of the methodBox.<Crayon>fillBoxes, knowing that type, the compiler knows that the first parameter in FillBoxes (U u) is a Crayon type and the second is a List of Boxes of Crayons (List<Box<U>> boxes).The method will fill a list of Crayon Boxes with red crayons 🙂
Ufff this was hard for a non Java programmer and non native english speaker…