0

How can i print variable name in Java?

Example:

String variableString = "some string";
System.out.println(variableName);

Expected result in console:

"variableString"

I need this for logging for variables like:

public By buttonLogin  = By.xpath("xpathExpression")
Filburt
  • 17,626
  • 12
  • 64
  • 115
Anton_Selenium
  • 339
  • 7
  • 20
  • 4
    This is more of a feature of a dynamic language. In a statically typed language like java, you really shouldn't ever care what a variable is called at runtime. If your program for some reason relies on it, your design is probably wrong. – Michael Apr 09 '19 at 10:47
  • @PhilippSander: really? This would break reflection. I doubt that. – Thomas Weller Apr 09 '19 at 10:49
  • Please note that there are as many "variableString"s as there are objects in the memory. It will not help printing the name of the variable, because you still don't know which object it is. – Thomas Weller Apr 09 '19 at 10:50

0 Answers0