Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

conditional compilation

 
kumud singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any provision of conditional compilation in JAVA as in C and C++ language?
Thanks in advance.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic