You can pass custom values in the command line, and then check for that value once. So in your code, have something like this:
final static boolean customProp = "true".equalsIgnoreCase(System.getProperty("customProp"));
Depending on your command line parameters, the static final
value will change. This will set the value to true
:
java -DcustomProp="true" -jar app.jar
While this will set the value to false
:
java -jar app.jar
This gives you the benefits of a static final boolean
, but allows the value to be altered without recompiling.
[Edit]
As indicated in the comments, this approach does not allow for optimizations at compile time. The value of the static final boolean
is set on classload, and is unchanged from there. "Normal" execution of the bytecode will likely need to evaluate every if (customProp)
. However, JIT happens at runtime, compiling bytecode down to native code. At this point, since the bytecode has the runtime value, more aggressive optimizations like inlining or excluding code are possible. Note that you cannot predict exactly if or when the JIT will kick in, though.