12

Is it possible to use String as a variable name.. like in this example -

String musicPlaying = "music2";
Music music1 = new Music("blaalla");
Music music2 = new Music("blalala");
Music music3 = new Music("balaada");

if(!musicPlaying.stillPlaying) { // As you can see i am using string as a variable name.
  changeMusic();
}
10
  • 1
    I don't understand your question. In the example you're not doing that. You're calling the constructor. Commented Jan 7, 2014 at 14:46
  • I am sure your question is not express in your code. Commented Jan 7, 2014 at 14:47
  • 2
    @RuchiraGayanRanaweera Java doesn't support dynamic naming for variables. Commented Jan 7, 2014 at 14:47
  • No this is not possible. But you can use a Map<String, Music> map. And then do if(map.get(musicPlaying).stillPlaying). Commented Jan 7, 2014 at 14:49
  • See this. Commented Jan 7, 2014 at 14:50

5 Answers 5

15

What you can do is by associating (mapping) those values to the Music object. Here is example:

Map<String, Music> musics = new HashMap<>();
String musicPlaying = "music2";
musics.put("music1", new Music("blaalla"));
musics.put("music2", new Music("blalala"));
musics.put("music3", new Music("balaada"));

if(!musics.get(musicPlaying).stillPlaying) { // As you can see i am using string as a variable name.
  changeMusic();
}
Sign up to request clarification or add additional context in comments.

2 Comments

@IsraelG. If you don't want a NPE to be thrown, make sure that musics.get(musicPlaying) does not return null. Music m = musics.get(musicPlaying); if(m != null && !m.stillPlaying) { changeMusic(); }
@ZouZou: If you properly encapsulate stuff, though, musicPlaying will always be a valid key (making musics.get(musicPlaying) always be a Music) -- and if ever it isn't, that's an error. You don't necessarily want to check for null, cause in the long run, you're better off getting a NPE in such cases than sweeping it under the rug with a null check.
6

You can't do this in Java, but you can almost do it using a map.

Map<String, Music> map = new HashMap<String, Music>();
map.put("music1", music1);
map.put("music2", music2);
map.put("music3", music3);

if(map.get(musicPlaying).stillPlaying) {
  // happy listening 
}

Comments

3

No, this is not supported in Java.

stillPlaying doesn't exist as a method (or variable) on String.

As the comment suggests below, it probably is doable through some reflection, however to quote another comment...

You can do all kinds of stupid tricks with reflection. But you're basically breaking the "warranty void if removed" sticker on the class the instant you do it.

2 Comments

Woo! I've been quoted! :)
doffs cap to your superior reflection knowledge :D
1

No. But you might want to look into using a Map instead.

1 Comment

yeah people already showed me.. btw i like your profile image, game of life, just like the old days huh? when programming looked like a game :)
-4

I used a switch case.

Switch (string)
{
    case "string1":
    string1();
    break;
    case "string2":
    string2();
    break;
}

3 Comments

Can you explain how that is related to OP's question?
The string is dynamic. We never know what it's going to be, but whatever it is, there's another variable of the same name, that is probably set when the string variable is populated with something. So to "use a string value as a variable name" then switch case it with the variable (or method) name called according to the string.
Read again. OP's question is not about Strings in general but dynamic variable names. A switch case of a String doesn't solve the problem. A switch case of "variable names" (not values) would help but that's not possible in Java as everyone has pointed out above.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.