How can the generic method called know the type of the generic return?

Posted by Paulo Guedes on Stack Overflow See other posts from Stack Overflow or by Paulo Guedes
Published on 2010-03-17T20:34:30Z Indexed on 2010/03/17 20:41 UTC
Read the original article Hit count: 193

Filed under:
|
|

I couldn't find a duplicate for this question for Java, although there are a lot of them for C#.

I have this method:

public <T> T getSomething() {
 //
}

According to the type of T, I will have a different return. For example:

String a = getSomething();
int b = getSomething();

For a, my method will return a specific String. For b, it will return a specific int. And so on.

It seems that this can be done with typeof() in C#. How can I achieve it in Java?

© Stack Overflow or respective owner

Related posts about java

Related posts about generics