Is there any provision of conditional compilation in JAVA as in C and C++ language? Thanks in advance.
posted 15 years ago
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.