Dynamic variable names Java

Use a normal HashMap with variable names as strings against their values. Or use a EnumMap with enums as key and your value as values. AFAIK, that's the closest you can get when using Java. Sure, you can mess around with reflection but IMO the map approach is much more logical.


You can use a Map<String, String> and locate the value by its key.

Even better, you can have an enum:

public enum Foo {
    S_R("Standard", 240),
    S_W("Standard", 180),...;

    private String type;
    private String duration;

    // constructor and getters
}

And then call Foo.valueOf(name)

(You can also do this via reflection - Constants.class.getField(fieldName) and then call field.get(null) (null for static). But that's not really a good approach.)


If you really must do this (and it's unlikely), you would have to use the Java "reflection" APIs.