25

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.

Code Poet
  • 11,227
  • 19
  • 64
  • 97
  • 2
    why do you have to ensure there are no `//` comments? -- The `//` inside a block comment is still legal syntax. also there is no reason you should be using `#if 0 ...` in C. that's what block comments are for – tobyodavies Nov 28 '11 at 08:24
  • For the second part, if I understand this correctly - Can't the block quotes be used for single line comments? – TJ- Nov 28 '11 at 08:24
  • 1
    Related: http://stackoverflow.com/questions/4526113/java-conditional-compilation-how-to-prevent-code-chunks-to-be-compiled – Thilo Nov 28 '11 at 08:25
  • Apologies, I seem to be mistaken. We don't have to do it. – Code Poet Nov 28 '11 at 08:25

9 Answers9

26

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

ante
  • 1,045
  • 10
  • 29
  • Does this only work if the static final is a boolean? Will using `DEBUG` as an integer work? – Pacerier Sep 06 '14 at 15:03
  • 1
    @Pacerier No it works for `static final int`s also but not `enum`s. I checked with decompiler, enums remain as is, but yes, `int`s can reference other final `int`s. Also `switch-case` with `int`s don't do the magic - i.e., plain `if...else if...` ladders This should have been the answer! – zeropoint Apr 24 '15 at 18:54
  • 1
    @zeropoint, However does the specs guarantee that this would indeed be compile-time? – Pacerier May 24 '15 at 22:37
  • Just checked if it changes the actual bytecode. It does, the whole part in if is missing if the statement is always false. – Ghandhikus Oct 27 '16 at 16:56
  • _the whole part in if is missing_ The if clause is removed as well right? – Doopy Apr 15 '18 at 19:17
  • @DQQpy Yes, the if statement is also removed. – ante Apr 17 '18 at 19:21
10

There are no pre-processor directives in Java. Your best choice is commenting out code.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
3

There are several solutions:

  1. Use a preprocessor - I think that it will work with the standard CPP. There were some Java specific proprocessors as jappo and java+ that you can try

  2. Replace the #if 0 with a true if:

    if(false) { code }

The condition can be refined by querying the system properties:

 if(System.getProperty("NO_COMPILE").equals("true")) {
    code
  } 

This has the advantage that it can be easily set either from ANT or from Eclipse.

EDIT: Please remark that with if the code will actually be compiled and present in the .class files. Moreover, although querying system properties is more elegant, is done at runtime not at compile time hence is not quite inline with original requirement. The if(false) is on a 2nd tought better.

EDIT2: an even better solution I have just found: http://prebop.sourceforge.net/

Daniel Voina
  • 3,185
  • 1
  • 27
  • 32
  • putting it in a if(false) block does not work in eclipse. For example the following gives be compiler error (I have Project->Build Automatically "ON"): if (false){ intentional;} which means it is being compiled. – Code Poet Nov 28 '11 at 18:11
  • That's right - this is what I said in the first edit. Even the solution with static final WILL compile code. Eclipse SHOULD NOT give errors but warn you about "Dead code". If you have errors then it is probably something wrong in the block guarded by if(false) – Daniel Voina Nov 29 '11 at 15:35
1

if(false) works well for me (using Eclipse).

  • 3
    Can you give an argument on *why* this is not compiled? – alestanis Oct 28 '12 at 19:30
  • AFAIK, this is compiled. So you can't have code errors within the `if` block. I've tried the `static final boolean DONT_COMPILE = false` and then using `if (!DONT_COMPILE){}`. This too still compiles the code in the `if` block. – Code Poet Oct 30 '12 at 05:11
1

you can just use Java Preprocessor and I guess it will be better way than flags http://code.google.com/p/java-comment-preprocessor/ flags of course allow to cut and to add blocks on the compilation phase but preprocessor allows to make the process much more flexible

Igor Maznitsa
  • 833
  • 7
  • 12
1

I can't even begin to imagine why this is an issue (as described) but I suspect the easiest thing to do is to grep for // in the code before you compile it (or commit it to your versioning repo). I don't think there's anything in eclipse (if that's what you're using) to help you and I'm almost positive that java has no built in mechanism like the one you're describing in C.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
1

I saw this somewhere a while ago:

// /*
class SomeClass{
     int withSomeField;
     ..............
}
// */

And you can put the //s in and and remove them as necessary. It's probably multiline comments that would cause problem if they were in there, though.

Vlad
  • 18,195
  • 4
  • 41
  • 71
-1

Instead of trying to make the code conditional by preprocessing the source-code (as C does), use object oriented programming: use design patterns such as Strategy, and dependency injection, to make code conditional.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
-2

There's not a Java-equivalent of the preprocessor's directive used in C/C++ languages. Anyhow, you could do something similar using annotations or ANT, as briefly explained here

Using annotations to add C like preprocessor directives

or here

How to do Conditional Compilation with Java

loscuropresagio
  • 1,922
  • 15
  • 26