vendredi 22 mai 2015

java compilation error when using reflection with templates

I have this code:

private <D extends Object> void foo(D d) throws IllegalAccessException, InstantiationException {
    d = d.getClass().newInstance(); // Compilation error: "Incompatible types: Required D, Found: Object"
}

Why do I get this error?

To my understanding, the compiler knows that d is D type (which extends Object)...?





Aucun commentaire:

Enregistrer un commentaire