As discussed here, javac and other Java compilers may provide code elimination capabilities for if
-statements where the condition is a "Constant Expression".
How is this affected if my code uses a constant expression that depends on other constant expressions defined in different packages?
For example, let's say I have the following classes in the respective specified packages:
package foo;
public class Foo {
public static final boolean CONDITION = false;
}
and
package bar;
import foo.Foo;
public class Bar {
public void test() {
if (Foo.CONDITION) {
System.out.println("This line of code could be eliminated.");
} else {
System.out.println("This line of code will be executed.");
}
}
}
Clearly, if the foo
-package is loaded at run-time from an external jar-file, the compiler can't technically just assume that Foo.CONDITION
will be false and should not eliminate the true
-branch of the if
-statement.
Whereas, if Foo
and Bar
were actually in the same package, the true
-branch should definitely be eliminated (if the compiler supports code elimination at all).
Not quite sure how to best phrase this question, but: How "close" does Foo
need to be to Bar
for a constant expression in Foo
to also be considered constant in Bar
? Would they need to be in the same file? the same package? the same jar-file? or does it not matter at all (i.e. would the compiler always consider Foo.CONDITION
as constant and use the value found in the build-path during compile time)?
javap -c Bar
. – Mistletoe