1

I have got a list of constants in a class like:

public class constants{
    public String avg_label ="......";
    public String count_label="......";
}

While calling , is it possible to do something like this:

public class MapDialogue{

    Constants c1 = new Constants();

    public String mappingLabels(String node){
        String text = "c1."+node+"_label";

        //Is there someway of parsing this text as code
        //like in unix shell scripting?
    }
}
Jonathan
  • 20,053
  • 6
  • 63
  • 70
vhora
  • 320
  • 2
  • 16
  • 1
    Yes it's called [reflection](http://docs.oracle.com/javase/tutorial/reflect/member/fieldValues.html) - but you might prefer to redesign your code so that you don't need to do that at all... – assylias Aug 19 '13 at 14:06
  • Possible duplicate of [http://stackoverflow.com/questions/935175/convert-string-to-code](http://stackoverflow.com/questions/935175/convert-string-to-code) – Martin Fahl Aug 19 '13 at 14:07
  • 1
    @MartinFahl That is not a duplicate at all... – assylias Aug 19 '13 at 14:08
  • @assylias-I have used reflection, but here it seems too much trouble for such a simple operation. Actually I had written a few shell scripts and now I am migrating the code to java. Any ideas on redesign the code. – vhora Aug 19 '13 at 14:15
  • About ideas how to redesign, what you are trying to achieve looks a lot like a dictionary, which in Java is implemented as a Map. Look at java.util.HashMap. – Cyrille Ka Aug 19 '13 at 14:16

2 Answers2

2

Yes, you could use Reflection. And the code would look something like this:

String value = (String)Constants.class.getDeclaredField(node).get(c1);

Although, I am kind of unsure about a couple of things:

  • Why are the constants in your class not really constants? (Constants are supposed to be static and final)
  • Why are you even instantiating your Constants class? You should be accessing them like Constants.FIELD_NAME.
  • You might want to take assylias's advice in the first comment and try to avoid using reflection at all times, surely there are other ways that you could do it that are less costly.

I suppose in your case, you would most likely be better of using some sort of Map

Josh M
  • 11,611
  • 7
  • 39
  • 49
  • I have not declared them as constants, because I wanted the user to have the flexibility to change the labels or customize them. – vhora Aug 19 '13 at 14:27
  • Well, it appears your class name seems to be misleading then. – Josh M Aug 19 '13 at 14:31
  • Hey I tried with reflection, and Its working fine. I have got around 200 variable names. Any ideas on what is the correct thing to do? Reflection or hashmap? – vhora Aug 19 '13 at 14:34
  • Well, a `HashMap` would definitely be more practical instead of reflection. You could put what is supposed to be the field name as the key, and its corresponding value as, well, the value. It would look something like this: `map.put("avg_label", ".....");` – Josh M Aug 19 '13 at 14:39
0

Yes, there is a way to achieve that. You can do so via the Reflection package in the Field API. The Java tutorials for this can be located here.

The basic idea is:

Field yourField = c1.getClass().getDeclaredField(yourString);

As a side note, a Constants file usually has its members as public static final. With those modifiers, you won't need to create an instance of Constants, and the values will also be unmodifiable.

asteri
  • 11,402
  • 13
  • 60
  • 84