Java generic return type not used in parameters
generics, java
Solution
The difference is that you can use this special version for method chaining.
Check this sample program:
public class MyFoo {
public <T extends MyFoo> T foo(){
System.out.println("foo");
return (T) this;
}
static class MyBar extends MyFoo{
public <T extends MyBar> T bar(){
System.out.println("bar");
return (T) this;
}
}
public static void main(String[] args) {
final MyFoo foo = new MyFoo().foo();
final MyBar bar1 = new MyBar().foo();
final MyBar bar2 = new MyBar().bar();
}
}
The assignment of the bar1 variable is only possible because of the `<T extends MyFoo>`, without it, there would have to be a cast:
final MyBar bar1 = (MyBar) new MyBar().foo();
Unfortunately, at least with Java 7, compiler inference ends here, so you can't do
new MyBar().foo().bar();
Problem
In a java library I came across a method which uses a generic return type that is not used in any way in the parameters: ``` <T extends ResponseCallBack> T sendData(@Nonnull final OrderIf checkoutOrder, @Nullable final List<NameValueIf> ccParmList) throws Exception; ``` (`ResponseCallBack` is an interface here) What is the difference with this signature: ``` ResponseCallBack sendData(@Nonnull final OrderIf checkoutOrder, @Nullable final List<NameValueIf> ccParmList) ```