0

Is is possible to create an new local variable from value of another?

e.g. if value of var1 = "button1" can I construct a new local variable like button1type, ie.using the value of var1 to make part of the new variable

3
  • I think that's not possible in languages like Java. Commented Dec 20, 2011 at 15:21
  • Where do you need this for? I think there will probably be a better solution for your problem. Commented Dec 20, 2011 at 15:36
  • Depending on which button I press, I pass a value via extras and then using this need to make variable names to write to correct col in database. I can just use switch, case and have separate update database sql. Thanks anyway. Commented Dec 20, 2011 at 15:46

1 Answer 1

3

Like this?

String foo = "ohai_" + var1; // Would be "ohai_button1"

If you mean name the variable based on the value in var1? No, but you don't need to.

If you need to associate data based on a string (or other) value, consider using a map.

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

1 Comment

@user1095784 I mean a map, HashMap is one implementation, and a typical choice.

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.