0

I'm using a ContentResolver query to get some data from a database in Android. That's not the issue.

The method returns string representation of integers,

INT TYPE_MAIN = 2

I want to convert that to a string Type_Main

String a = someMagicalMethod(TYPE_MAIN);
System.out.println(a);

Such that the output would be

TYPE_MAIN

I can't use Integer.toString(TYPE_MAIN) because that would return the value of it which is 2

How can I achieve this?

Ozair Patel
  • 1,658
  • 1
  • 12
  • 17

2 Answers2

3

In Java, you cannot inspect values of variables by using their name, that info is not available at run time, not even through reflection. Use a Map<String, Integer> to solve your problem:

Map<String, Integer> map = new HashMap<>();
map.put("TYPE_MAIN", 2);
//...
String a = map.get("TYPE_MAIN").toString(); //someMagicalMethod(TYPE_MAIN);
System.out.println(a); //prints 2
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I'm using that method right now but I want to optimize my code, and converting the representations to strings would help me do that. If I can't do it I'll use this method, thanks. – Ozair Patel Jun 03 '15 at 21:04
0

Make it an Enum, then you can use String a = TYPE_MAIN.getName();

FredK
  • 4,094
  • 1
  • 9
  • 11