Skip to content

Instantly share code, notes, and snippets.

@evant
Last active August 29, 2015 14:17
Show Gist options
  • Save evant/5492a2fe34ac241f5a21 to your computer and use it in GitHub Desktop.
Save evant/5492a2fe34ac241f5a21 to your computer and use it in GitHub Desktop.
Why doesn't this compile?
public class Wtf {
public static abstract class Foo<V> { // Removing this type parameter allows it to compile
public <T> T bad(Class<T> fooClass) {
return null;
}
}
public static class Bar {
}
public static class Baz extends Foo {
public <T> T good(Class<T> fooClass) {
return null;
}
public void go() {
Bar badBar = bad(Bar.class); // This won't compile
Bar goodBar = good(Bar.class); // But this will
}
}
}
@evant
Copy link
Author

evant commented Mar 13, 2015

javac 1.7.0_75 to compile java sources
Errors occurred while compiling module 'Wtf'
Compilation completed with 1 error and 0 warnings in 1 sec
src/com/example/Wtf.java
Error:(22, 29) java: incompatible types
required: com.example.Wtf.Bar
found: java.lang.Object
src/com/example/Wtf.java uses unchecked or unsafe operations.
Recompile with -Xlint:unchecked for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment