2

I am attempting to reference a variable using a certain string but have no idea how to do it. I know that I can use if statements if I really had to but I am sure that there is a simple way. An example is a Integer named dog. I would try to access the Integer using another string that contained the text dog.

  private int dog;
    String anything = "dog";

Is there anyway this is possible? Thanks!

5
  • @AVD I think he's going to need a bit more explanation than that. Perhaps you could post a code sample? Commented Dec 18, 2013 at 2:53
  • 5
    Gosh, this has never been asked before. Sigh... Commented Dec 18, 2013 at 2:54
  • It's possible (through either a map or reflection), but could you justify why you want to do this? Commented Dec 18, 2013 at 2:55
  • I am creating a server for a game and need to reference each player through their specific client ID, in order to update their position. Commented Dec 18, 2013 at 3:05
  • 1
    @Silver: then use a map, but don't try to use variables this way. Variables are not nearly as important as you think and in fact almost don't exist in compiled code. What matters most is references and that is what a map will do for you. Search the site for similar questions (something you should have done to begin with since this questino gets asked daily). Commented Dec 18, 2013 at 3:07

4 Answers 4

2

Try this:

// use a map for referring to a value given its name
Map<String, Integer> vars = new HashMap<String, Integer>();

// for example, let's use these values
String anything = "dog";
int dog = 10;

// bind a value to a name
vars.put(anything, dog);

// retrieve the value, given its name
vars.get(anything);
=> 10
Sign up to request clarification or add additional context in comments.

Comments

0

http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

public static void main(String[] args) {
    Map<String, MyObject> mapping = new HashMap<>();
}

Or new HashMap<String, MyObject>(); for pre java 7

Comments

0

You should use a Map of String to Integer. For example,

public static void main(String[] args) {
  java.util.Map<String, Integer> dogMap = new java.util.HashMap<String, Integer>();
  dogMap.put("Snoop", 10);
  dogMap.put("doggy", 15);
  dogMap.put("dog", 20);

  System.out.println(dogMap);
}

Which outputs

{doggy=15, Snoop=10, dog=20}

Comments

-1

Two options: create a Map<String, Object> that connects the two, or use reflection. I prefer reflection.

in order to get the field:

public class Test {
    private int dog = 10;
    private String anything = "dog";

    public static void main(String[] args){
        Test obj = new Test();
        Object field = obj.getClass()
                          .getDeclaredField(obj.anything)
                          .get(obj);            
        System.out.println(field);
    }
}

Output:

10

Create an object of the class that you will use. Then use the getDeclaredField() method on the class of that object. This will look into the private fields that are set, getField() holds only the public fields. That's it.

I've removed the try-catch from the post because it just clutters it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.