I just did some testing on the client and it seems to perform this optimization, but I think there’s a quite annoying limitation.
for example, when you have in your class a
final boolean DO_SOMETHING_MORE = false;
and elsewhere you do somewhere in a tight loop or something
if (DO_SOMETHING_MORE) {
system.out.println(“Doing something more”);
}
this is correctly optimized away.
But, if you set this final boolean in the constructor like this:
public MyClass(boolean doSomethingMore) {
this.DO_SOMETHING_MORE = doSomethingMore;
}
and you instantiate this class like this
MyClass myInstance = new MyClass(false);
this optimization is not done.
It seems like either this optimization is done only by javac, or if such optimizations are done in runtime (which doesn’t seem so) it just fails to optimize this away if the final boolean is set in the constructor. Whatever the case, the optimization seems only to be performed in a static context.
(EDIT: when it’s a static constant, not a constant in an instance)
In any way, this seems like a really annoying limitation for me because I wanted to be able to optimize a CPU emulator if possible by adding a flag in the constructor like this CpuEmulator(boolean strictEmulation) where the CPU will be emulated a little bit more loosely (for example no cycle counting and no addressing exceptions) if strict emulation is set to false.