In Java, is there a way to create variable labels using another variable in a for loop:for(int x= 1; x < 3; x++){int y_x = 0;} | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In Java, is there a way to create variable labels using another variable in a for loop:for(int x= 1; x < 3; x++){int y_x = 0;}

[Begginer] The code above of course does not declare the variables y_1 and y_2. Is there a way to say that x is the variable x and not the character x?

21st Dec 2018, 3:06 PM
Marcelo
Marcelo - avatar
3 Answers
+ 3
I was tempted to do this when i was learning c, but i cant remember why because there are no valid reason to do so. Because you can make y an array
21st Dec 2018, 3:40 PM
Taste
Taste - avatar
+ 2
No, there's no way. Variable names are static. They cannot be composed dynamically. By the way, the code will never enter your for loop. The condition evaluates to false with the initial value of x.
21st Dec 2018, 10:01 PM
bullion
+ 1
Thank you all. In fact, I mistook > for <. As a begginer, I was trying to figure out how to build an array and attribute values to it in a for loop, but without knowing the number of items beforehand. Maybe I'll learn to do that with another object (maybe a list) later. Thank you all once again
21st Dec 2018, 10:09 PM
Marcelo
Marcelo - avatar