Is it possible to create variables at runtime in Java?

后端 未结 6 1593
无人共我
无人共我 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:55

    You're not going to be able to modify a class that's already been loaded into the JVM. However, you could conceivably use ASM < http://asm.ow2.org/ > or BCEL < http://commons.apache.org/bcel/> to dynamically generate a new class that has the dynamically-defined fields.

    Way more trouble than it's worth. Seriously, just use a HashMap!

提交回复
热议问题