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

后端 未结 10 2339
醉酒成梦
醉酒成梦 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:19

    It looks like your professor is PHP-biased on the feature (Variable variables), so he was thinking if that was possible in java.

    I personally don't think that this is possible, not in the way you are proposing. What can be done is the generation of classes at runtime, using tools like Javassist to make a more powerful reflection mechanism. So you can create a class that has the variables you want (string1, string2, etc.) at runtime.

    However, don't forget that Variable variables is a really bad technique, which leads to bad code. It might be useful on very few cases, but I really don't recommend it.

提交回复
热议问题