On Wed, 12 Mar 2008 09:30:30 +0000, Michael Rogers wrote:
> On Mar 11 2008, Sven-Ola T?cke wrote:
>> P.S. While I'm not a Java expert - for unbloating software, normally an
>> #ifdef is a good thing [tm]. Any analog contructs with this funny
>> language?
>
> Dynamically loaded classes can be useful when you want to switch between
> implementations. If you want to enable/disable code you can use final
> variables (equivalent of consts in C) - the compiler (at least the Sun
> compiler) will optimise away unreachable parts of the code. The following
> code produces different bytecode depending on the flag, with only one of
> the strings included in the bytecode. But if you remove the 'final'
> modifier, both strings are included.
>
> class IfDefTest
> {
> final static boolean FLAG = true;
>
> public static void main (String[] args) {
> if (FLAG) System.out.println ("The flag is true"); else
> System.out.println ("The flag is false");
> }
> }
> }
> Cheers,
> Michael
Better make the flag priavte too ("private final static boolean"),
that way it won't be visible from any other class and can thus be
optimized away completely. Dunno if the Sun compiler is smart enough to do
so, thought.