question 21

Posted Thursday, September 03, 2009 by SacrosanctBlood in

Consider the below generic method:

public static <U> void fillBoxes(U u, List<Box<U>> boxes) {
        for (Box<U> box : boxes) {
            box.add(u);
        }
    }

Also, this snippet,

Crayon red = ...;
List<Box<Crayon>> crayonBoxes = ...;

Now,

What is the way to invoke the generic method?

a. Box.<Crayon>fillBoxes(red, crayonBoxes);
b. Box.fillBoxes(red, crayonBoxes);



1 comment(s) to... “question 21”

1 comments:

SacrosanctBlood said...

a. Box.<Crayon>fillBoxes(red, crayonBoxes);



This can be used to invoke the method. It is a proper syntax.



b. Box.fillBoxes(red, crayonBoxes);



This is also a proper syntax. It can too be used to invoke the method.
This feature, known as type inference, allows you to invoke a generic
method as you would an ordinary method, without specifying a type
between angle brackets. ‘red’ being a Crayon type, Type ‘U’ refers to
Crayon( :D Smart compiler..he he..).



Post a Comment