running the Demo class will invoke a static method newInstance in SomeClass to call the constructor and printing hello
defining a method will include a return type + method name with arguments
the return type for newInstance is <T>SomeClass<T> seems weird to me
since my class is called SomeClass<T> instead of <T>SomeClass<T>
why do i need the <T> in front of the SomeClass<T> ?
it seems that if I don't include it there will be an common error
called Cannot make a static reference to the non-static type T
another thing to point out is that I can put many spaces between <T> and SomeClass<T> so it doesn't seem like they need to be together.
public class SomeClass<T> {
public static <T>SomeClass<T> newInstance(Class<T> clazz){
return new SomeClass<T>(clazz);
}
private SomeClass(Class<T> clazz){
System.out.println("hello");
}
}
public class Demo {
public static void main(String args[])
{
SomeClass<String> instance = SomeClass.newInstance(String.class);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…