Dynamically read value of a constant in Java
This post will discuss how to dynamically read the value of a constant in Java.
1. Using Reflection
This can be achieved using Reflection in Java, which allows programmatic access to information about the fields of loaded classes. Note that this approach is not recommended, and we should avoid reflection at all costs.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class Numbers { public static final String NUM_1 = "ONE"; public static final String NUM_2 = "TWO"; public static final String NUM_3 = "THREE"; public static final String NUM_4 = "FOUR"; public static final String NUM_5 = "FIVE"; } class Main { public static void main(String[] args) { try { for (int i = 1; i <= 5; i++) { String var = "NUM_" + i; String value = Numbers.class.getDeclaredField(var) .get(null) // null for static .toString(); System.out.println(var + " has a value of " + value); } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } } } |
Output:
NUM_1 has a value of ONE
NUM_2 has a value of TWO
NUM_3 has a value of THREE
NUM_4 has a value of FOUR
NUM_5 has a value of FIVE
2. Using Enum
The recommended solution is to use Enum for storing constants, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
enum Numbers { NUM_1("ONE"), NUM_2("TWO"), NUM_3("THREE"), NUM_4("FOUR"), NUM_5("FIVE"); private final String value; Numbers(String value) { this.value = value; } public String getValue() { return value; } } class Main { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { String var = "NUM_" + i; String value = Numbers.valueOf(var).getValue(); System.out.println(var + " has a value of " + value); } } } |
Output:
NUM_1 has a value of ONE
NUM_2 has a value of TWO
NUM_3 has a value of THREE
NUM_4 has a value of FOUR
NUM_5 has a value of FIVE
3. Using Map
Finally, we can use a map with constant’s variable names as keys against their values.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.HashMap; import java.util.Map; class Numbers { private static Map<String, String> numbers; static { numbers = new HashMap<>(); numbers.put("NUM_1", "ONE"); numbers.put("NUM_2", "TWO"); numbers.put("NUM_3", "THREE"); numbers.put("NUM_4", "FOUR"); numbers.put("NUM_5", "FIVE"); } public static String valueOf(String key) { return numbers.get(key); } } class Main { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { String key = "NUM_" + i; String value = Numbers.valueOf(key); System.out.println(key + " has a value of " + value); } } } |
Output:
NUM_1 has a value of ONE
NUM_2 has a value of TWO
NUM_3 has a value of THREE
NUM_4 has a value of FOUR
NUM_5 has a value of FIVE
That’s all about dynamically reading the value of a constant in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)