How to Convert a string to variable name in Java

Hi ever body,
How can I use a string to name a variable?

for example for those who know php :smiley:

$a = 5;
$b = ${'a'};
echo $b;
// and the output will be 5

I need to do the same thing in Java but i don’t know how :injured:
Thank you in advance

In Java or JSP? Can you show us what you have so far?

I have nothing much just learning java as new language :rolleyes:

I’m not really sure what you’re trying to do but maybe the reflection API can help you. This is far from beginner Java though.

Why not just do:

int a=5;
int b = a;


This will output 5.

With reflection you can get a value of a field in a class.

public class SomeClass {
  public int a;
  public static void main(String [] args) {
    SomeClass s = new SomeClass();
    s.a = 5;
    try {
      int b = (Integer) SomeClass.class.getField("a").get(s);
    } catch  (Exception e)  {

Haven’t test it but it should output 5.

It sure did work. Thank you very much.