There are no preprocessor directives in Java, so in this case there is no option other than using IDE shortcuts for this (usually "soutv + tab", this works, for example, in InteliJ IDEA and Netbeans). Thus, you will get the result for the default template:
System.out.println("var = " + var);
Or you can implement a general method for this:
void printVariable (Object variable, String name) { System.out.println (name + " = " + variable); }
And run it like this:
printVariable (var, "var"); printVariable (anotherVar, "anotherVar");
UPDATE: according to this answer in Java 8 there might be a way to do this through reflection (in some cases)
source share