0

Possible Duplicate:
Dynamic Variable Names in Java:

Let's say I have a string, as below.

String s = "Hello";

Now, I want to create a string, but the string's variable will be called "Hello". In order to make the string's name "Hello", I must access string s to get the name "Hello", so I can use it as a variable name. Below is what I want to see.

String Hello = "I want to do this, But from Accessing String s So I KNOW that String s = Hello";

Thank you for the effort, and please try to explain to me because I am a Java beginner. :D

Community
  • 1
  • 1
  • 4
    1. Why do you want to do that? There probably is a better way to do what you want... 2. It can be done via the reflection API which is not easy to grasp for a Java Beginner. – assylias Sep 16 '12 at 12:15
  • 1
    Agree with @Sayem and voting to close. Poster please check out the link Sayem provides. As has been indicated here and in the link, this is not the *way* of Java. Better to use a `Map` if you want to associate a String variable with a String. – Hovercraft Full Of Eels Sep 16 '12 at 12:17
  • How can it be done using reflection ? There are no dynamic variables in Java. – Anton Sep 16 '12 at 12:17
  • @Anton: Reflection could work with class fields, not local variables. But again, this would be a terrible ugly kludge and is not recommended. To see how to do this, check the link that Sayem provided. – Hovercraft Full Of Eels Sep 16 '12 at 12:20
  • @Hovercraft Full Of Eels. Reflection can't help with creation of variable at runtime. Using reflection you can't access local variables. It's not even close to the OP code. – Anton Sep 16 '12 at 12:25
  • Could the OP possibly really need the message formatter? – bmargulies Sep 16 '12 at 12:30

5 Answers5

3

What you are attempting to do is add a layer of indirection. You cannot access variables dynamically in a static language such as Java/C/C++/Pascal/etc

What you can do is emulate the dyamic context that dynamic languages use by, eg creating a Map to hold the variable names and values in this case you would have

Map<String,String> stringVars = new HashMap<String,String>();

// set a "variable"
stringVars.put("Hello", "value");

// get a "variable"
System.out.println(stringVars.get("Hello"));
Stephen Connolly
  • 13,872
  • 6
  • 41
  • 63
2

Using Reflection (not recommended):

public class MainClass
{
    public String Hello = "I want to do this, But from Accessing String s So I KNOW that String s = Hello";

    public static void main(String[] args) throws Exception
    {
        MainClass m = new MainClass();

        String s = "Hello";
        String result = (String) MainClass.class.getField(s).get(m);
        System.out.println(result);
    }
}

OUTPUT:

I want to do this, But from Accessing String s So I KNOW that String s = Hello

Instead, use a map as others illustrated.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

It is not possible in java.

The only thing you can do is to use A map interface' implementation, for example a HashMap. Using a put method you can 'assign' a value to a given 'name'. The name would be a key and has to be unique within a map, just like variable has a unique name within it's scope.

To retrieve the value, call get method passing appropriate key (ex. string 'Hello') as an argument.

Krzysztof Jabłoński
  • 1,890
  • 1
  • 20
  • 29
  • 3
    It actually *is* technically possible with class fields, although not recommended. This sort of thing has been discussed over and over again here. – Hovercraft Full Of Eels Sep 16 '12 at 12:22
  • Agree, it is possible as shown in [Eng.Fouad's post](http://stackoverflow.com/a/12446884/1598508). However mostly any behavior can be achieved using basic java interfaces. Reflection should be used when is absolutely unavoidable. – Krzysztof Jabłoński Sep 16 '12 at 12:42
0

In Java you cannot obtain the variable(reference) name which is pointing to an object since the object has no knowledge of the reference to it and there can be more than one variable refering to the same object.

On the other hand you could do somtething like this; but I do not know how would help:

String s = "Hello";
Reference<String> hello = new SoftReference<String>(s);
String myStringAccessedWithHello = hello.get();
Random42
  • 8,989
  • 6
  • 55
  • 86
0

Could you use a Map which takes a string (such as "Hello") and maps it to another String (such as "I want to do this, But from Accessing String s So I KNOW that String s = Hello"). Something like the following:

Map<String, String> stringMap = new HashMap<String, String>();
stringMap.put("Hello", "I want to do this, But from Accessing String s So I KNOW that String s = Hello");

Then you can find all of the key values in the map (such as "Hello") by calling:

Set<String> stringKeys = stringMap.keySet();

and you can lookup the long string belonging to the key "Hello" like this:

String longValue = stringMap.get("Hello");

This is how I would use a simple String value to find an unwieldy String value. Bear in mind you could also use a Map which maps String values to any other type of object.

Bobulous
  • 12,967
  • 4
  • 37
  • 68