In ActionScript 2 and 3, we can access a member variable of a class like it is in an associative array.
for eg.,
//AS3 Code
private var tempvar:String="Hello";
function printString()
{
trace(this.tempvar);
trace(this[tempvar]);// would print the same as above
}
I was wondering if the same is possible in Java as well.
The reason I need this is because, I have some variables like the following in my java code
//java code
private String var_1, var_2, var_3, var_4;
To access these variables, I have to write 4 different statements
//java code
var_1="SomeValue1";
var_2="SomeValue2";
var_3="SomeValue3";
var_4="SomeValue4";
If it is in AS3, I could have done something like this
//AS3 Code
for( var i=1; i<=4;i++)
{
this["var_"+i]="SomeValue"+i;
}
I’ll be a much happy coder if someone could let me know how to do the same in Java.
Thanks
I think you need to refactor your Java code…
You can use a
Map<String, String>to achieve that.