title: Looking for an easier way to write debugging print statements in Java
body: <p>EDIT: I would love to read reactions to Steve Reed's AOP approach. Comments to his answer are encouraged!</p>
  
  <p>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:</p>
  
  <p>EDIT: fixed this. Used to be: var + ": " + var, which was totally wrong. Dumb typo.</p>
  
  <pre><code>System.err.println ( "var: " + var );
  </code></pre>
  
  <p>Later I learned that this was common practice. At least, where a debugger was unavailable or unwanted.</p>
  
  <p>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:</p>
  
  <pre><code>void dbug ( Object obj )
  {
      String variableName = obj.somehowGetVariableName();
      String variableContents = obj.toString();
      System.out.println ( variableName +": " + variableContents );
  }
  </code></pre>
  
  <p>But apparently getting the variable name is easier said than done.</p>
  
  <p><a href="https://stackoverflow.com/questions/744226/java-reflection-how-to-get-the-name-of-a-variable">java-reflection-how-to-get-the-name-of-a-variable</a></p>
  
  <p>Am I stuck with:</p>
  
  <pre><code>System.err.println ( "var: " + var );
  </code></pre>
  
  <p>Or is there a popular shorthand version of this?</p>