I have a generic class like this:
public class MyClass<T> {
private T var;
public MyClass(T init_value) {
var=init_value;
}
...
}
Now I would like to add toString()
method to it, which returns the concrete type and value of var
. I try something like this:
public String toString() {
return String.format("MyClass object initialized with type %s and value %s", T, var);
}
This, however, does not work, as I have a problem that type variable T seems not to be visible as normal variable. Namely, I get an error:
T cannot be resolved to a variable
How can I overcome this problem and make a type variable printable?