Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark java code such that it's not compiled [duplicate]

Tags:

I'm wondering if there if a Java equivalent for C's

#if 0 ... Some Code ... #endif 

Which can be used around code blocks we don't want to compile. Adding block quotes:

/* ... Some Code ... */ 

also has the same effect, but the problem is, we have to ensure there are no single line comments

// some comment 

in the block.

like image 952
Code Poet Avatar asked Nov 28 '11 08:11

Code Poet


People also ask

What is compile error Java?

Compile Time Errors are those errors which prevent the code from running because of an incorrect syntax such as a missing semicolon at the end of a statement or a missing bracket, class not found, etc. These errors are detected by the java compiler and an error message is displayed on the screen while compiling.

Is Java compiled twice?

Java code needs to be compiled twice in order to be executed: Java programs need to be compiled to bytecode. When the bytecode is run, it needs to be converted to machine code.


1 Answers

static final fields can be use for conditional compilation.

static final boolean DEBUG = false;  if (DEBUG) {   some code .... } 

some code will be removed by the compiler.

It is also possible to use the assert keyword to enable and disable some part of the code. Use java -ea: .. to control if the code should be enabled or disable. See http://docs.oracle.com/javase/1.5.0/docs/guide/language/assert.html

like image 158
ante Avatar answered Sep 21 '22 11:09

ante