Thanks for all of your responses. I'm frustrated that it is impossible
to create an instance of type T due to the type erasure of Java.
Or maybe I do not express my idea clearly. All I want is the following
class:
class InputClass
{
public
{
String str = getInputFromConsole();
T v = null;
// v = new T(str);
// how to do this?
return v;
}
}
well, client can call InputClass's getValue() with Integer, or Double,
or something else that I don't know.
My idea is that if I can get the instance (or Class class) of type T,
I can get object v by call T's valueOf() method. If I can not get the
instance (or Class class) of type T, the class will be like:
class InputClass
{
public Integer getIntegerValue()
{
String str = getInputFromConsole();
return new Integer(str);
}
public Double getDoubleValue()
{
String str = getInputFromConsole();
return new Double(str);
}
// other getXxxValue()...
}
It's almost useless and very ugly!