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

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

    Naming variables like that looks very 1980-ish. Meaning pre object oriented programming. So if you ever build software for a living - DON'T DO THIS.

    But since it seems to be homework...

    When we're talking about a named variable in Java, we mean something that's compiled. Unlike in some scripting languages there is no easy way to do this in Java.

    So either you use a runtime compiled class like Markus Lausberg suggested.
    Or you cheat and use the Java Scripting API and make use of the scripting languages. That way you can create code (in a String) at runtime.

提交回复
热议问题