javawildcardmethod-parameters

Wildcards as method attributes in java


Is it possible to use wildcards as an attribute itself and not as a type parameter?

public void doSomething(<? extends Context> myObject){


}

I want to make sure that my object extends Context and also implements a certain interface.

Using wildcards as a return type is valid but using it as an attribute seems to not work

public <? extends MyClass> validMethod(){


}

Solution

  • Yes it's possible, you have to define it before the return type.

    public <T extends MyClass> void myMethod(T param);