Reputation: 6749
In Java, when I get a ResultSet
I made a method that would map that to a specified class. It more or less looks like this:
public <T> T mapRow(ResultSet results, Class<T> type) {
So, when I actually call the class, it looks like this:
mapRow(results, Row.class);
Please help me understand this conceptually.
mapRow<Row>(results);
?mapRow
, is there an implied <Row>
before the method call?Upvotes: 0
Views: 1426
Reputation: 200168
the current way I'm invoking mapRow, is there an implied before the method call?
The point of the <T> T mapRow()
declaration is that T
is inferred from the method arguments or the LHS type to which the result is being assigned. In your case, you pass in a class object which serves to infer the return type.
The above is only one half of the purpose of the class argument, the second part being reflection: mapRow
will call newInstance()
on it to create the object to return.
Upvotes: 2
Reputation: 43087
You can call:
object.<Row>mapRow(results, Row.class);
but you still need the second parameter anyway, you cannot remove it for what you are trying to do.
This is because there is no way to know the type of T at runtime when mapRow is called, due to generics erasure.
Upvotes: 3