0

In C/C++ we can use #ifndef RELEASE #endif to include debug source such as some extra tests and/or console print and etc

Is there any way to do it in java or any similiar alternative ?

1
  • Doing this would be a bad hack. Logging should be done through a configurable logging mechanism with different log levels. Tests should be done in unit tests. Commented Nov 22, 2012 at 10:39

1 Answer 1

5

You can add a static final boolean variable and then use simple if statements.

If the compiler can determine that the expression is false then it will not compile the conditional into your code.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 I learned something cool today. Is this true for any compiler of any version ? I mean is it in the JLS
At least the Oracle/Sun Compiler do it. I don't know since which version, but at least recent compiler (> 1.4) do it.
@Jerome It is not in the JLS as it is just a compiler optimization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.