How smart is in Java if instructions with final variables

I would like to write troubleshooting code that I can easily remove from later versions of my program debugging. I figured it out:

final static boolean debug_on=true;
...
if (debug_on) { system.out.println() or logger.log(...) }

Is Java smart enough to discard the if statement from the final bytecode if debug == false?

Is there a better practice to achieve the goal of saving debug code from the final version of the program?

+5
source share
1 answer

See the end of the Java language section, section 14.21. Unreachable Statementments for Description if(false):

if (false) { x=3; }

. x=3; , x=3; "" , .

, " ", :

static final boolean DEBUG = false;

, :

if (DEBUG) { x=3; }

, DEBUG false true true false, , - .

tl; dr; , if -.

+5

All Articles