Why we cant invoke listFactory with type parameter ?, but can with List?
Instance Creation In a class instance creation expression, if the type is a parameterized
type, then none of the type parameters may be wildcards. I think its the same like:
List<?> list = new ArrayList<?>(); // compile-time error
Only top-level parameters in instance creation are prohibited from containing wildcards.
Nested wildcards are permitted. Hence, the following is legal:
List<List<?>> lists = new ArrayList<List<?>>(); // ok
This is a reason why:
SClass.<List<?>>listFactory();//ok
From Java Generics and Collections
Generic Method Calls If a generic method call includes explicit type parameters, those type
parameters must not be wildcards.
class SClass{
public static <T> ArrayList<T> listFactory(){ return new ArrayList<T>(); }
}
You may choose for the type parameters to be inferred, or you may pass an explicit type
parameter. Both of the following are legal:
List<?> list = Lists.factory();
List<?> list = Lists.<Object>factory();
If an explicit type parameter is passed, it must not be a wildcard:
List<?> list = Lists.<?>factory(); // compile-time error
Nested wildcards are permitted:
List<List<?>> = Lists.<List<?>>factory(); // ok
The Java designers had in mind that every
wildcard type is shorthand for some ordinary type, so they believed that ultimately every
object should be created with an ordinary type. It is not clear whether this restriction is
necessary, but it is unlikely to be a problem.