To use a string value as a variable name [duplicate]

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();
}

Solution 1:

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();
}

Solution 2:

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 
}