In Java there's no preprocessor directives, so in this case there's no option, except for using IDE's shortcuts for this (usually, "soutv + tab", this works, for example, in InteliJ IDEA and Netbeans). So you'll get the output for default template:
System.out.println("var = " + var);
Or you may implement the common method for doing this:
void printVariable (Object variable, String name) {
System.out.println (name + " = " + variable);
}
And run it in this way:
printVariable (var, "var");
printVariable (anotherVar, "anotherVar");
UPDATE: according to this answer, in Java 8 there might be a way to do it through reflection (in some cases)