The simplest solution is this:
private final String[] REQ_DROPDOWNS = {
"//div[text()='XXX']", "//div[text()='YYY']"};
// NB: I have changed the argument type to 'int'
public Boolean goodVar(int num) {
if (num > 0 && num < REQ_DROPDOWNS.length) {
return this.IsVisble(REQ_DROPDOWNS[num - 1]);
} else {
throw new IllegalArgumentException("Num out of range: " + num);
}
}
Java does not support dynamic variables; see https://stackoverflow.com/questions/6729605.
You could dynamically lookup a Field
and then access it using reflection, but it is more complicated and error prone to do that.
You could also use a HashMap
, but that too is unnecessarily complicated for the use-case in your example. But if you wanted the name lookup to be more flexible, this would be a good option.
private final Map<String, String> map = new HashMap<>(){{
put("REQ_DROPDOWN_1", "//div[text()='XXX']");
put("REQ_DROPDOWN_2", "//div[text()='YYY']");
}}
public Boolean goodVar(String suffix) {
String path = map.get("REQ_DROPDOWN_" + suffix);
if (path == null) {
throw new IllegalArgumentException("Unknown suffix: " + suffix);
}
return this.IsVisble();
}