Java Reference
In-Depth Information
Then, we can compile and run this normally and the debugging statement is omitted. But if
we run it with a
-D
argument to enable
debug.fetch
, the printout occurs:
>
java starting.Fetch
# See? No output
>
java -Ddebug.fetch starting.Fetch
Fetching poem
>
Of course this kind of
if
statement is tedious to write in large quantities. I originally encap-
sulated it into a
Debug
class, which remains part of my
com.darwinsys.util
package.
However, I currently advise the use of a full-function logging package such as
Logging with log4j
)
, or similar.
This is as good a place as any to interject about another feature—inline code generation. The
C/C++ world has a language keyword
inline
, which is a hint to the compiler that the func-
tion (method) is not needed outside the current source file. Therefore, when the C compiler is
generating machine code, a call to the function marked with
inline
can be replaced by the
actual method body, eliminating the overhead of pushing arguments onto a stack, passing
control, retrieving parameters, and returning values. In Java, making a method
final
enables
the compiler to know that it can be inlined, or emitted in line. This is an optional optimiza-
tion that the compiler is not obliged to perform, but may for efficiency.
See Also
“Conditional compilation” is used in some languages to enable or disable the printing or
“logging” of a large number of debug or informational statements. In Java, this is normally
the function of a “logger” package. Some of the common logging mechanisms—including
ones that can log across a network connection—are covered in Recipes , , and .