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?
    }
}
5
  • 1
    Yes it's called reflection - but you might prefer to redesign your code so that you don't need to do that at all... Commented Aug 19, 2013 at 14:06
  • Possible duplicate of http://stackoverflow.com/questions/935175/convert-string-to-code Commented Aug 19, 2013 at 14:07
  • 1
    @MartinFahl That is not a duplicate at all... Commented Aug 19, 2013 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. Commented Aug 19, 2013 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. Commented Aug 19, 2013 at 14:16

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

I have not declared them as constants, because I wanted the user to have the flexibility to change the labels or customize them.
Well, it appears your class name seems to be misleading then.
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?
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", ".....");
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.

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.