Father-in-law of Stackoverflow :
My main aim is to achieve the following syntax while calling the static method by any means.
MyClass.staticMethod<Type>();
This is what I have achieved so far,
static <T> void MyClass()
{
/* Function Body */
}
Andreas :
If you have a generic class like
public class MyClass<T> {
public MyClass() {
}
}
which is normally instantiated using new
new MyClass<Type>()
but you don't want the caller to use new
, you want a static
method to do that, then do it like this:
public class MyClass<T> {
public static <T> MyClass<T> newInstance() {
return new MyClass<T>();
}
private MyClass() {
}
}
The caller can now do:
MyClass.<Type>newInstance()
though the Type
can generally be inferred:
MyClass<Type> myObj = MyClass.newInstance();