Do we have conditional compiling in java like c?

Do we have a replacement of these kind of statement in java ?

#ifdef MacOSX
    #import <Cocoa/Cocoa.h>
#else
    #import <Foundation/Foundation.h>
#endif

I want to compile my java classes depending upon the conditions?

Jon Skeet
people
quotationmark

No, Java doesn't have anything like that. While the compiler can strip out code which is guaranteed not to be executed, it's still got to be valid code. So you can have:

private static final FOO_ENABLED = false;

...

if (FOO_ENABLED) {
    System.out.println("This won't be included in the bytecode");
}

... you can't have:

private static final FOO_ENABLED = false;

...

if (FOO_ENABLED) {
    This isn't even valid code. With a real preprocessor it wouldn't matter.
}

You can run "not quite Java" code through a regular preprocessor in order to get valid Java afterwards, but that will be painful when developing.

It's better to abstract out the functionality you're interested in into interfaces / abstract classes, and then have different concrete implementations for different platforms, picking the right one at execution time.

people

See more on this question at Stackoverflow