tags:

views:

189

answers:

5

I have a Java array:

    String[] myArray = {"1", "2"};

Depending on a condition that is known at compile time I would like to assign different values:

    String[] myArray = {"A", "B", "C"};

In C++ I would use something like

#ifdef ABC
  // ABC stuff here
#else
  // 123 stuff here
#endif

but what to do in Java?

+1  A: 
String[] myArray;

if (ABC)
myArray = ...
else
myArray = ...

ABC is static final variable, JVM is required to inline condition check.

stepancheg
+3  A: 

In Java you would need to do that at runtime.

String[] myArray;
if (something)
    myArray = new String[]{"A", "B"};
else
    myArray = new String[]{"A", "B, "C"}

This is going to be the most similar to C++ code. And, if your condition is guaranteed to be true at compile time, Java will optimize out the call.

There are other options available, but they will look nothing like a C++ version.

jjnguy
Thanks, this will definitely work - will the compiler optimize away the else part if "something" is always true??
Actually yes. If you have that something so it is always true, it will optimize away the `if.`
jjnguy
+1 given that the `something` would be a `static` `final`, so the `if` disappears, this is clearly the best suggestion so far. And therefore, obviously, it was downvoted!
Daniel Earwicker
(And much more clearly worded than stepancheg's version)
Daniel Earwicker
Of course...I totally deserved the downvote. Thanks for noticing!!
jjnguy
Using this I got "Array constants can only be used in initializers", but myArray = new String[]{"A", "B"}; resolved the issue.
+1: this is basically my answer
dfa
oh, thanks for pointing out my syntax error.
jjnguy
A: 

One approach is to re-generate a Constants.java file in the first phase of your build script (ant,maven or whatever else you use). Depending on how complex that file is you can do it manually or use something more heavy like Velocity.

I used something similar in the past to put version and build info inside java classes.

Gregory Mostizky
+6  A: 
class Foo {

   static final boolean ABC = true;

   public void someMehod() {
       if (ABC) {  // #ifdef ABC

       } else {    // #else

       }           // #endif
   } 
}

since ABC is both static and final the compiler evaluates it at compile-time, effectively acting like a pre-processor.

dfa
Ouch, stolen out from under me! `:P`
jjnguy
Pushing for 10k today? Good luck.
jjnguy
thanks, I've reached the 200pt limit today :(
dfa
Bummer, you must be in a different timezone, because I just got up today.
jjnguy
Ahh, Italy. Yup. Other side of the world.
jjnguy
Use libt's idea from the comments. use System.getProperty("...") to assign ABC.
James Schek
A: 

Use a template engine (something like Velocity) to execute a preprocessing step. Your build script could run the template applying properties from the build environment to output Java source code that is ultimately compiled. This is, essentially, what C/C++ preprocessors do.

My guess is that someone has already coded an ant task to do this, but I'm too lazy to Google it for you.

Jeff Knecht