Search code examples
javagenericsgeneric-programming

Generic methods T extends


I want to do something like that:

public final <T>T getObject(Class <T extends MyObject> myObjectClass){
    //...
}

IDE complains about syntax error. How to write this correctly?


Solution

  • You declared the generic type bound in the wrong place.

    It should be declared within the declaration of the generic type parameter:

    public final <T extends MyObject> T getObject(Class<T> myObjectClass)
    {
        //...
    }