Q:

How to leave out some System.out/logging statements while executing java code?

In C++ if we do not want some statements to compile into code that ships like assert function calls, we control their compilation through #ifndef preprocessor directives.
How do we do this in Java?
I have some System.out.println() statements for debugging which I would like to remove for the final code.
one way is to make them execute conditionally under the affect of a boolean variable. Is there a better way of doing this?
As I have a java swing application I can turn off the System.out.println statements without affecting the output. What is the method of doing this?

A:

Use a logging framework like slf4j. You can print to the console in debugging and omit everything in production without recompiling the application.

A:

Use logging. See log4j or commons logging.
Generally, each log entry has a severity level (like debug, info, warning, error) and you can configure which are printed from the application. You can print all of them for debug, but only some (e.g. info and higher) in production. The configuration is usually done with a single plain text file. 
Logging frameworks can do more than that: Add more detail automatically (e.g. timestamp or thread ID), log to console, file and/or database, rotate files, and more.

A:

Use AspectJ for those pieces of code which you want added or removed at compile time and compile without AspectJ when you don't want to use them.

