Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I must cast to Generic Type T even if I know it returns correctly?

Tags:

java

generics

My code:

private static <T> T get(Class<T> clazz) throws IllegalAccessException, InstantiationException {
        if (clazz.equals(String.class)) {
            return (T) new String("abc");//line x
        } else {
            return clazz.newInstance();
        }

    }

As you see, in line x, T have to be String.class and returns String. But compile failed without casting the result to T.

Change line x to return new String("abc"); results Incompatible types.

like image 977
Sayakiss Avatar asked Oct 22 '15 08:10

Sayakiss


People also ask

Should you always return the most specific type when designing methods?

I guess most developers heard the guideline stating that, when designing methods, you should return the most specific type and accept the most generic one. Is it always applicable? Let’s try to look at it from different perspectives. Let’s start with some simple example. Let’s say we have the following class hierarchy: And the following method:

Should you use generic types for input parameters?

Of course, it’s not always possible to use generic types for input parameters. Otherwise, we would always use Object as the type of the method parameters. That brings us to the second part of the guideline: make your methods accept parameters of the most generic types possible.

Is it possible to return a value of a specific type?

It is not always possible to return a value of a specific type (e.g. Employee or Customer). For example, if you have some polymorphic collection with objects of both types, you might have no choice other than returning an object of Person type:

Should we change the return value type of person methods?

Changing the return value type to a more generic one puts too many restrictions to the method consumers. In the worst case, the client code would need to manually cast the Person object to an Employee, so it is better to not make them do that. It is not always possible to return a value of a specific type (e.g. Employee or Customer).


1 Answers

The compiler does not take into account the if statement.

So all it sees is that you need to return a T (of which it has no further knowledge). It did not infer that T must be String here.

You can avoid the warning that you get for the "unchecked cast to erased type" by doing

return clazz.cast("abc");
like image 134
Thilo Avatar answered Oct 06 '22 20:10

Thilo