Is it possible to set a variable, if i want to have it flexible? I think an exmaple makes it easier to understand.
String hallo1;
String hallo2;
for(int i = 0; i < 2; i++) {
hallo & i = Integer.toString(i);
}
No, you cannot. There's (fortunately) no such thing as eval()
in Java.
Your best bet is to grab an Array or an ArrayList. Here's an Array example:
String[] hallos = new String[2];
for (int i = 0; i < 2; i++) {
hallos[i] = Integer.toString(i);
}
It's technically possible with reflection, but I would advise against it unless you have a really good reason to do it. It would make your code much more difficult to debug and to read.
EDIT:
As the comments stressed:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With