Jump to content

Conditional code inclusion at build time


Recommended Posts

Posted

I'm new to Java programming, although luckily it's similar enough to C/C++ that I am so far having no difficulties.

However, one thing I used a lot in C is the preprocessor, which allows you to specify build options in a way that makes it easy to switch on or off different builds. The preprocessor runs at build time before the compiler and you can pass it options to do certain things, most commonly define substitution macros.

As an example, you have something like

#define __DEBUG_BUILD__
//#undef __DEBUG_BUILD__ /* Switch debugging on or off at build time */
#ifdef __DEBUG_BUILD__
#define log_info( /*  Whatever variables you want */ ) /* Some code to substitute for log_info */
#else
#define log_info /* log_info is now a blank expression - no debug info will be logged */
#endif

Does Java have something similar to the C preprocessor?

Posted

Java does not have such a thing as It's a higher level language.  Just declare it as a (public)static final and boom, there  you go
Reason: Java doesn't have a precompiler. I know it will be annoying to wrap your head around but eventually, you'll get used to it. You could make your own version of it using classes and all, but it won't make any difference since the won't preprocess it anyway... 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...