#if in java, as in c preprocessors

Possible duplicate:
How to mark Java code so that it does not compile

In c , we can prevent compilation of block code as follows:

#if 0 //code here #endif 

So even if a code block with an error is prone to compiling code, I want the same thing in Java so that I can skip the part of the code that will not compile because some library is missing.

Can anybody help me?

+9
source share
6 answers

You must comment on the code , you cannot use the preprocessor directive in java.

+12
source

There is no preprocessor in Java. Depending on your build system, you can use a third-party preprocessor (you can find many of them by searching for “Java preprocessor”). Some examples

Depending on the area you want to comment out, you can use block comments and sometimes something like

 if (false) { . . . } 

If all else fails, just comment out each line using // . Most IDEs have a way to do this (and undo it) efficiently.

PS If you can use block comments (this is not always possible, since block comments cannot be embedded in Java), there is a good trick to minimize the amount of work that needs to be done to disable and leave the commented part out. Start your block comment on a separate line, but end it with another isolated line starting with a line comment, for example:

 /* <block of code ignored as comment> //*/ 

Then, if you want to re-enable the commented section, just add a second / at the beginning of the block:

 //* <block of code now active> //*/ 

To disable the code again, simply delete the first / .

+17
source

I assume that the compiler will block code inside blocks with forced / final flags? Here is how you can leave the code in your project that is not sent to the final apk.

 public final static boolean DEBUG = false; if(DEBUG) { //add messy error prone code here //compiler will strip this out as unreachable when false } 

Read here:

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21

+6
source

In Java, there is no preprocessor for C lines.

Since, according to the tags, you are using Eclipse, you can simply mark the entire block of code that you want to comment on, then use CTRL - / .

You can also use the same key sequence to uncomment an already commented out block.

+1
source

One of the methods I used is to use m4 as a preprocessor for java files. You write your code in classname.java.m4 and use a Makefile or other build system rule to run m4:

 %: %.m4 @echo "/* automatically generated from $< -- don not edit*/" > $@ m4 $< >> $@ 
+1
source

java Do not provide pre-processor facility.

By the way, you can use the comment as below:

you are using Eclipse, you can simply mark the entire block of code you want to comment on, then use CTRL + SHIFT + C for comments and uncommentation of the block of code.

+1
source

All Articles