You can download a Java decompiler and investigate yourself. I suggest
this one:
http://java.decompiler.free.fr/?q=jdgui

You'll see that it does indeed optimize when it can.

However, unlike a C file which gets linked into a .so or .dll or an
executable, Java classes may be combined in different ways.

So it cannot optimize away static final references in other classes,
because those may change.

However, tools like ProGuard can do so, because they work on an entire
application.

But remember, once you do something like this, that code can never be
used in a context with a different valiue.

It virtually never makes sense to do this for conditional compilation
reasons. Sometimes it makes sense to generate code (the Android tools
do in fact generate a couple of files). In the rare case you do need
to eliminate code due to extremely tight performance constraints or
size constraints, I'd recommend the ProGuard route, as it will do a
FAR more thorough job than you can do manually with conditional
compilation.

On Mar 30, 8:12 am, "Kevin S." <[email protected]> wrote:
>   It really isn't very easy to do this.  In general, you can do
> something like
>
> public class MyClass
> {
>    public static final boolean  HAS_CHEEZBURGER=true;
>
>    public void someMethod()
>    {
>        if( MyClass.HAS_CHEEZBURGER)
>        {
>            doCheezburger();
>        } else
>        {
>            noCheezBurger();
>        }
>    }
>
> }
>
>    The compiler I think will see that HAS_CHEEZBURGER is constant and
> can never change, and may optimize away the branch not taken.  Perhaps
> some more knowledgeable folk could comment on that.
>
>   If on the otherhand, you really really think you need to do
> preprocessing using eclipse and Java, you can.   See the following
> link.
>
> http://www.boldinventions.com/index.php?option=com_content&view=artic...
>
> -Kevin
>
> On Mar 30, 5:57 am, Dilip Dilip <[email protected]> wrote:
>
>
>
> > Hi All,
> >  How to do conditional compilation in JAVA.
>
> > something like in C, C++ :
>
> > #ifdef TRUE_CONDITION
>
> > #else
>
> > #endif
>
> > Thanks and Regards,
> >   Dileep

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe from this group, send email to 
android-developers+unsubscribegooglegroups.com or reply to this email with the 
words "REMOVE ME" as the subject.

Reply via email to