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();
}
julian
  • 4,634
  • 10
  • 42
  • 59
  • 1
    I don't understand your question. In the example you're not doing that. You're calling the *constructor*. – Maroun Jan 07 '14 at 14:46
  • I am sure your question is not express in your code. – Ruchira Gayan Ranaweera Jan 07 '14 at 14:47
  • 2
    @RuchiraGayanRanaweera Java doesn't support dynamic naming for variables. – Maroun Jan 07 '14 at 14:47
  • No this is not possible. But you can use a `Map map`. And then do `if(map.get(musicPlaying).stillPlaying)`. – Alexis C. Jan 07 '14 at 14:49
  • See [this](http://arshajii.com/coding-faqs/dynamic-vars.html). – arshajii Jan 07 '14 at 14:50
  • @ᴍarounᴍaroun The example explains it best, if you still cannot understand the question, ahmmm, go see a doctor? jk thanks for helping! – julian Jan 07 '14 at 14:54
  • @IsraelG. That's a little bit rude. I would clarify that, not replying with that useless comment. I suggest you to see a psychologist. (After all, I just wanted to help) – Maroun Jan 07 '14 at 14:55
  • @ᴍarounᴍaroun I am sorry if you took it personally, I didn't mean to be rude, hahah thanks for the suggestion, you will be disappointed if I'll refuse? have a nice day :) – julian Jan 07 '14 at 14:59
  • @IsraelG. Sorry for misunderstanding you my friend :) Maybe that's because of too much work today.. Have a nice day! And sorry if I was rued.. Best wishes! – Maroun Jan 07 '14 at 15:02
  • @ᴍarounᴍaroun Its ok, you wasn't rude, its my bad, if you worked too much today, just listen to some good music, relax and everything will be great, have a nice day :) lets not spam the chat here.. otherwise we gonna be reported :) – julian Jan 07 '14 at 15:09

5 Answers5

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();
}
Wins
  • 3,420
  • 4
  • 36
  • 70
  • 1
    @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(); }` – Alexis C. Jan 07 '14 at 14:56
  • @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. – cHao Sep 08 '14 at 07:51
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 
}
Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
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.

Community
  • 1
  • 1
David
  • 19,577
  • 28
  • 108
  • 128
1

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

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • 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 :) – julian Jan 07 '14 at 14:55
-4

I used a switch case.

Switch (string)
{
    case "string1":
    string1();
    break;
    case "string2":
    string2();
    break;
}
rupweb
  • 3,052
  • 1
  • 30
  • 57
  • Can you explain how that is related to OP's question? – runDOSrun Jul 07 '15 at 13:26
  • 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. – rupweb Jul 07 '15 at 17:41
  • 1
    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. – runDOSrun Jul 07 '15 at 20:58