Skip to content

Instantly share code, notes, and snippets.

@mark-jay
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save mark-jay/8860012 to your computer and use it in GitHub Desktop.

Select an option

Save mark-jay/8860012 to your computer and use it in GitHub Desktop.
public class ReturningGeneric {
public static class A{}
public static class B{}
@Test
public void method1() {
A a = method2(); // so good so far
B b = method2(); // error here
System.out.println(a + " " + b);
}
private <T> T method2() {
try {
T res = (T) new A();
return res;
}
catch (ClassCastException cce) {
return (T)new B(); // unreachable statement
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment