Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I print out the name of the variable?

I have created a no. of constant variables, more than 1000, those constants are unique integer.

public static final FOO  335343
public static final BAR  234234
public static final BEZ  122424
....
....
....

Is there a way to print out the FOO, BAR and BEZ, the variable of the names in Java? I am not familiar with java reflection. I don't know if that helps.

if ( FOO == 335343)
   ---> output "FOO"
if ( BAR == 234234 )
   ---> ouptut "BAR"
....

Actually asking this question behind is that I want to write log into the file

say

System.out.println("This time the output is " + FOO);

and the actual output is

This time the output is 335323

I want to know which variable comes from 335323. Is there any other way apart from putting those variable and its associate constant into hashMap?

Thanks

like image 711
TheOneTeam Avatar asked Jul 27 '11 01:07

TheOneTeam


People also ask

Can Python print variable names?

You can't, as there are no variables in Python but only names.

Can you print a variable name in Java?

In short. You can't print just the name of a variable.

How do you print the representation of a variable?

Python __str__() This method returns the string representation of the object. This method is called when print() or str() function is invoked on an object.


1 Answers

There are some 'special case' that u can have workaround for this (which is told by other), but the most important question is: why would you want to do this (printing out variable name)?

From my experience, 99.9% of similar questions (how to print variable name? how to get variable depends on user inputting variable name? etc) is in fact raised by beginner of programming and they simply have made incorrect assumptions and designs. The goal they are trying to achieve normally can be done by more appropriate design.


Edit

Honestly I still do not think what you are trying to do is the way to go, but at least I think the following is a workable answer:

It is more or less a combination of previous answer:

(Haven't try to compile but at least it give u an idea)

class Constants {
  public static final int FOO = 123;
  public static final int BAR = 456;

  private static Map<Integer, String> constantNames = null;


  public static String getConstantName(int constVal) {
    if (constantNames == null) {
      Map<Integer, String> cNames = new HashMap<Integer, String>()
      for (Field field : MyClass.class.getDeclaredFields()){
        if ((field.getModifiers() & (Modifier.FINAL | Modifier.STATIC)) != 0) {
            && int.class == field.getType()){
          // only record final static int fields
          cNames.put((Integer)field.get(null), field.getName());
        }
      }
      constNames = cNames;
    }
    return constantNames.get(constVal);
  }
}

assuming you want to get a constant name, just do:

Constants.getConstantName(123);  // return "FOO"
like image 105
Adrian Shum Avatar answered Sep 20 '22 23:09

Adrian Shum