Controlling debugging output

TUTOR HOME
PREVIOUS
TIPS HOME
NEXT
HELP BUY CONTACT ABOUT

Java doesn't have a #define as C does. But you can control debugging output this way:

static boolean debug = false;

if (debug) { System.out.println ("Wrote " + wdata); }

You can also make debug a byte and assign it values, then test debug for its value, similar to warning levels in a compiler. You can use this to emit 'light' debug information or 'verbose' as needed.

Having the debug statements compiled into the code can be desirable - then your code can dynamically set the debugging on/off but you can have the Java compiler not include the debug statements in the class file if you declare the debug variable final:

static final boolean debug = false; // This eliminates debug statements
static final boolean debug = true; // This includes the debug statements

- thanks to Ralph Iden -