Answer the question
In order to leave comments, you need to log in
Why does a class generic affect a method's own generic?
Question by example:
static class Foo<T> {
<R> R get(R value) {
return value;
}
}
static {
{
// Так работает:
Foo<?> foo = new Foo<>();
Long a = foo.get(123L);
}
{
// Так работает:
Foo<String/*любой тип*/> foo = new Foo<>();
Long a = foo.get(123L);
}
{
// А почему так не работает?
Foo foo = new Foo();
Long a = foo.get(123L); // -- incompatible types Long и Object
}
}
Answer the question
In order to leave comments, you need to log in
If you use a raw reference, you lose the ability to use generics for any non-static class members through that reference. JLS(4.8)
Combining Raw Types and Generic Methods
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question