I have an idea.
Make a testVariabletype Object(or a class DummyTypethat extends Object). You can then load the variable with whatever data you want using primitive wrapper classes, depending on what you read from your system variable.
So:
public class Test {
Object testVariable;
{
String whichType = null;
if(whichType.equals("int")) {
testVariable = new Integer(intVal);
}
else if(whichType.equals("double")) {
testVariable = new Double(doubleVal);
}
}
Of course, this is not Java “figuring out” what type it is at compile time, as you want, it is necessary (and the assignment will be executed at runtime when the object was created Test), but it seems to be a reasonable basis for an alternative.
And you can also set the value testVariableduring initialization, if necessary, naturally.
, , String ( ) - :
public Object getPrimitiveValue(String type, String value) {
if(type.equals("int")) {
return new Integer(Integer.parseInt(value));
}
else if(type.equals("double")) {
return new Double(Double.parseDouble(value));
}
}