For the below code
public class T<T> {
public String toString() {
return "Generic Types!";
}
public void run(T var) {
Integer repeatCount = 12_57;
System.out.println("debug: " + repeatCount + ", " + var.toString());
}
public static void main(String[] args) {
System.out.println("Hello, World!");
T<String> t = new T();
t.run(t);
}
}
The line public class T<T>
gives a warning The type parameter T is hiding the type T<T>
.
The line T<String> t = new T()
gives a error cannot make a static reference to non-static type T
.
It works fine with below output, if I change public class T<T>
to public class T<K>
. But I don't understand the reason.
output :
Hello, World!
debug: 1257, Generic Types!
I am new to the concepts of Generics , can someone please explain what is wrong with the above code ?