File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes conditional compilation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "conditional compilation" Watch "conditional compilation" New topic

conditional compilation

kumud singh

Joined: Apr 11, 2000
Posts: 5
Is there any provision of conditional compilation in JAVA as in C and C++ language?
Thanks in advance.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
The closest equivalent is to create a public static final boolean variable in the class you're
working with. Then use that variable as the conditoin in an it statement:

The compiler will still look at the code inside the conditional to see if it parses, but if at compile time it is certain that the condition is false (hence the need for the variable to be final) it won't generate any code for the conditional part.
The other rather bizarre solution, if you want conditional compilation just like you get in C/C++, is to run your java code through a C preprocessor before you feed it to the javac compiler. The preprocessor should execute all the #ifdef statements you want, while ignoring your funky Java code. Probably.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: conditional compilation
It's not a secret anymore!