Is there away to generate Variables' names dynamically in Java?

后端 未结 10 2345
醉酒成梦
醉酒成梦 2020-11-28 14:46

Let\'s say that I need to generate variables to hold some input from the user (I don\'t know how many they are). Without using Array, ArrayList (an

10条回答
  •  暖寄归人
    2020-11-28 15:17

    I think you can generate a Java class at runtime or maybe use some script engine like Beanshell to generate the variables, you can even build the class by its bytecode. But I can't see how you will use that variables in your code, you must also create the code to work with that variables, or use reflection for that...

    A naive solution:
    create a class with all variables from var000 to var999 with a getter for each... but that's not really dynamically!

提交回复
热议问题