This might not have a major usecase in projects, but I was just trying a POC kind of project where in I get the key code, and using its value I want to print the key name on screen.
I want to relive myself off writing switch cases, so thinking of going by reflection.
Is there a way to get the constant integer of interface’s name using its value?
KeyPressed(int i) {
string pressedKeyName = getPressedKey(i);
System.out.println(pressedKeyName);
}
I can think of two better solutions to this than using reflection.
Any decent IDE will auto-fill in switch statements for you. I use IntelliJ and it does this (you just press ctrl-enter). I’m sure Eclipse/Netbeans have something similar; and
Enums make a far better choice for constants than public static primitives. The added advantage is they will relieve you of this problem.
But to find out what you want via reflection, assuming:
Run:
Output: