Is it possible to create variables at runtime in Java?

后端 未结 6 1563
无人共我
无人共我 2020-11-28 15:22

For example, say I wanted to \"extract\" String[] fruits = {\"Pear\", \"Banana\", \"Apple\"}; into three separate variables, eg:

for (int i=0; i         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 15:49

    The question is not why you want to do it but 'what are you going to do with it?'. So suppose at runtime variable with the name fruits2 magically appeared on the stack of your method. Now what? You had to know its name at compile time to take advantage of it. Reflection will not help you access local variables.

    Anyway, I would be interested if you described more detailed use case.

提交回复
热议问题