0

Is it possible to create a method that can create objects named by String?

private static int a = 0;
public static void CreateNew() {
    Object ("number" + a) = new Object();
    a++;
}

If you know, how would I do it?

2
  • What language is this, Java? Commented Mar 24, 2013 at 13:24
  • What are you using this for? Is it something that you could just achieve by having a hashmap holding the objects that are have the string as their key? Commented Mar 28, 2013 at 1:42

1 Answer 1

1

It makes more sense to use a java.util.Map / HashMap in this case.

Map<String,String> strMap = new HashMap<String, String>();
strMap.put("number" + a, "value_for_this_string");

Java is a compiled language and not an interpreted language like the scripting languages where you can do something like this straightforward.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.