EDIT: I would love to read reactions to Steve Reed's AOP approach. Comments to his answer are encouraged!
I'm a novice, and at some point I realized it would be helpful to know the contents of a variable during program execution. So I started doing this:
EDIT: fixed this. Used to be: var + ": " + var, which was totally wrong. Dumb typo.
System.err.println ( "var: " + var );
Later I learned that this was common practice. At least, where a debugger was unavailable or unwanted.
I use a basic text editor, and typing the print statement every time I need to debug a variable is pretty tiresome, so I thought, why not something like this:
void dbug ( Object obj )
{
String variableName = obj.somehowGetVariableName();
String variableContents = obj.toString();
System.out.println ( variableName +": " + variableContents );
}
But apparently getting the variable name is easier said than done.
java-reflection-how-to-get-the-name-of-a-variable
Am I stuck with:
System.err.println ( "var: " + var );
Or is there a popular shorthand version of this?