1

lets pretend I have the following:

public class Something{

    private final String VALUE_AAA = "ABC";
    private final String VALUE_BBB = "DEF";
    private final String VALUE_CCC = "GHI";

    public String getValue(String param) {

    }
}

Now I want if I pass the value BBB to getValue in return DEF

Is that possible? I don't want if - else statements. I tought about BeanUtils but I am not sure.

Thanks, Hauke

5
  • Have you considered using a values array? Commented Aug 27, 2011 at 6:57
  • no that is not an option. I made this example easier with numbers, but in my project I need to put a string at the end of value_ instead of a number. For example VALUE_AAA, VALUE_BBB, VALUE_CCC and I pass to the method BBB. Commented Aug 27, 2011 at 7:09
  • You could then use a HashMap. The only language (that I know of) where you can do this in a (relatively) straightforward fashion is PHP. Commented Aug 27, 2011 at 7:12
  • Okay I thought about that also, but I hoped that something like this would work "String x = VALUE_+param;" Within script languages that works. I think I will do it with the map solution. Thanks :-) Commented Aug 27, 2011 at 7:15
  • If you're OK with reflection you can use it to achieve exactly what you need. See below. Commented Aug 27, 2011 at 7:35

3 Answers 3

3

Maybe you could use a HashMap,

public class Something{

    protected final HashMap<Integer, String>() hashMap = new HashMap<Integer, String>(){{
        put(new Integer(1),"ABC");
        put(new Integer(2),"DEF");
        put(new Integer(3),"GHI");
    }}

    public String getValue(Integer nr) {
        return hashMap.get(nr);
    }
}

EDIT

It seems these set of values are preintialized in class itself, For String parameter as input you could use:

public class Something{

protected final HashMap<String, String> hashMap = new HashMap<String, String>(){{
    put("AAA","ABC");
    put("BBB","DEF");
    put("CCC","GHI");
}};

public String getValue(String nr) {
    return hashMap.get(nr);
}

}

I've skipped the "Value_" since if you decide to use the HashMap, you wouldn't need a variable name as such, just the key.

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

3 Comments

a) pass a String ("BBB"), b) missing keyword 'return'. c) in () hashMap the parens are wrong. If you remove them, it still doesn't compile.
@user unknown: If you did note, the question is a edited revision with a String parameter as input from an Integer asked by the OP.
No, I didn't mention. The rules of SE discourage to change the meaning of an edit, so I don't inspect the history of a question, before answering and reacting. However, rerurning return new String [] {"AAA", "BBB", "CCC"}[param-1]; would then have been sufficient, and, btw., your Code doesn't compile.
0

You can use reflection: Get the current object's class, lookup the field by its name, then extract this field's value at the current object:

public class Something {

  private final String VALUE_AAA = "ABC";
  private final String VALUE_BBB = "DEF";
  private final String VALUE_CCC = "GHI";

  public String getValue(String param) throws Exception {
     return (String) this.getClass().getDeclaredField("VALUE_" + param).get(this);
  }
}

More details: http://download.oracle.com/javase/1,5.0/docs/api/java/lang/Class.html#getDeclaredField(java.lang.String)

3 Comments

you probably need getDeclaredField() because the fields are private.
unreported exception java.lang.NoSuchFieldException; must be caught or declared to be thrown
Thanks @deadsven and userunknown. Fixed.
0

Sorry, working code:

public class HundM
{
    protected final static HashMap <String, String> hm = new HashMap <String, String> (); 

    public static String getValue (String param) {
        return hm.get (param);
    }

    public static void main (String args[])
    {
        hm.put ("AAA", "ABC");
        hm.put ("BBB", "DEF");
        hm.put ("CCC", "GHI");
        System.out.println (getValue ("BBB"));
    }
}

2 Comments

Observe the question he isn't expecting the values to be dynamically put but rather as a literal, plus whats the point of adding the "Value_" if you are using HashMap. But otherwise your code would 've been just fine.
Yes, "VALUE_" was somehow a stupid idea. Thank you. What do you mean "dynamically put"? That's just not the scope of the question. A datastructure (HashMap) and a form to get the values in and out. Whether he wants to add more values to the hashmap later or not isn't part of the question, as I understood it.

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.