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?
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?
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.
No. In general you'll want to use a common superinterface for the functionality that you'd want to provide by Cocoa
/Foundation
, and then have two different implementing classes for that interface, depending on the platform you're targetting.