#Ifdef #Ifndef in Java

#ifdef #ifndef in Java

private static final boolean enableFast = false;

// ...
if (enableFast) {
// This is removed at compile time
}

Conditionals like that shown above are evaluated at compile time. If instead you use this

private static final boolean enableFast = "true".equals(System.getProperty("fast"));

Then any conditions dependent on enableFast will be evaluated by the JIT compiler. The overhead for this is negligible.

#ifdef equivalent in Java?

You have to declare the variable at the class-level, if you want the variable to be visible to all the methods in that class.

If your doing the following inside a method:

private static final boolean DEBUG = false;

the problem is that the modifiers private and static are not allowed within a method.


Actually, you should be using logger for this kind of purpose.

Implement #ifdef in Java

 Class ErrorVerbose { 
private static boolean enabled = False;

public static setEnable(boolean enable)
enabled = enable;
}

public static perror(String msg) {

if (enabled) {
/* Print */
}

}
}

class YourClass {

public YourClass(....,boolean status) {

ErrorVerbose.SetEnable(status)
.
.
}

}

Well, yes, either you can make is a non static class so that other classes can enable / disable verbose. Advantage of making a ErrorVerbose class is that you add more info like time, date, function name (which called), etc which makes it more informative I just gave a skeleton.

Java equivalent of #ifdef that allows non-compilable code

As others have said, the answer to your actual question is no.

However, you might approach your problem by isolating the Android or desktop code. You could do this by having three separate projects in eclipse:

  • Core: This is the "shared" code that exists between both versions.
  • Android: This contains only the code that runs on Android.
  • Desktop: This contains only the code that runs on desktop.

Both your Android and Desktop projects would contain the Core project on their classpaths. In eclipse, you'd do this by going to your Java Build Path, then clicking the Projects tab, then adding the Core project to the "Required projects" list.

Then you'd set your code up so your Android and Desktop projects are what you actually deploy, and your Core project contains the code shared between them. Here's a simple example. Let's say we have an example class that looks like this:

public class Adder{
public void addAndPrint(int x, int y){

//code that will work on both Android and desktop
int sum = x+y;

if (ANDROID){
//code that will only work on Android
Log.v("example", "Sum:" + sum);
}
else{
//code that will only work on desktop
System.out.println("Sum: " + sum)
}
}
}

You could get around this by refactoring your code to isolate the "core" code that will work on both desktop and Android. Something like this:

//example core class
public class CoreAdder{

Printer printer;

public CoreAdder(Printer printer){
this.printer = printer;
}

public void addAndPrint(int x, int y){
int sum = x+y;
printer.print("Sum: " + sum);
}
}

//example core interface. We might print differently on
//Android and Desktop, so implement this interface in each.
public interface Printer{
public void print(String printMe);
}

Then, you'd isolate the code that will only work on Desktop:

//on desktop, use System.out.println()
public class DesktopPrinter implements Printer{
public void print(String printMe){
System.out.println(printMe);
}
}

//on desktop, entry point is main()
public class DesktopMain{
public static void main(String... args){
DesktopPrinter printer = new DesktopPrinter();
CoreAdder adder = new CoreAdder(printer);
adder.addAndPrint(1, 2);
}
}

And the code that will only work on Android:

//on Android, use a logger
public class AndroidPrinter implements Printer{
public void print(String printMe){
Log.v("example", "index=" + i);
}
}

//on Android, entry point is Activity
public class AndroidActivity extends Activity{

public void onCreate(Bundle savedInstanceState) {
AndroidPrinter printer = new AndroidPrinter ();
CoreAdder adder = new CoreAdder(printer);
adder.addAndPrint(1, 2);
}
}

Note that this is just an example, and I know that both System.out.println() and Log.v() could work on either platform. But the idea is the same: split your project up into multiple projects, and use interfaces to abstract away the behavior that changes between platforms.

#if in java, like in c preprocessors

You have to comment out the code, you can't use pre-processor directive in java.

#define in Java

No, because there's no precompiler. However, in your case you could achieve the same thing as follows:

class MyClass
{
private static final int PROTEINS = 0;

...

MyArray[] foo = new MyArray[PROTEINS];

}

The compiler will notice that PROTEINS can never, ever change and so will inline it, which is more or less what you want.

Note that the access modifier on the constant is unimportant here, so it could be public or protected instead of private, if you wanted to reuse the same constant across multiple classes.

How to void if/else preprocessor macro with another define?

Just treat the two variables separately and determine the logic for when each should be defined:

#if defined(A) || defined(B)
int a;
#endif
#if !defined(A) || defined(B)
int b;
#endif


Related Topics



Leave a reply



Submit