The problem they are discussing is the compiler optimizations. javac
will find any static final primitives, and instead of using a look up
to get the value, will actually compile in the value.

So, with the situation as:

char val = NamingContainer.SEPARATOR_CHAR;

the compiled code is actually:
char val = ':';

There is no reference to the NamingContainer anymore. This is really
bad for when APIs are broken. It is why I have seen a lot of people
use:

public static final char VALUE = new Character('c').charValue();

Although not ideal, it stops the compiler optimizations. The problem
with NamingContainer, is that it is a ':' char, and therefore most
code will need to be re-compiled if that contants value is changed (no
matter what it is changed to).

The other issue with using a system property is that it is
non-standard. Most configuration like this is usually in web.xml. This
also makes sure that one war file can do it one way and another war
file can do it another (would be important if your company is
deploying 3rd party war files on the same web server).

-Andrew

On 4/21/06, Adam Winer <[EMAIL PROTECTED]> wrote:
> No, I know - but that isn't what I was saying.  Code that is
> compiled against JSF 1.1 will simply have ":" inlined,
> and will not re-consult the value in NamingContainer.
> This technique would only work if you got people to
> recompile against JSF 1.2.
>
> And people are going to be compiling against 1.1 for a
> long time.
>
> -- Adam
>
>
> On 4/20/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> >
> > > You'd still have to recompile all code to take advantage of that.
> >
> > Er, no you don't. The value of the constant is only set when the class is
> > loaded. It will use the current value of the system property, which gets
> > set when the JVM is initialized, i.e. before any classes get loaded.
> > Anything that was compiled to that constant will use the value of the
> > constant as defined when the class is loaded. It is a hack, but it avoids
> > changing the API...
> >
> > For example:
> >
> > public interface MySysProps {
> >       String CONST = System.getProperty("myprop", "default");
> > }
> >
> > public class SysPropsTest {
> >       public static void main(String[] args) {
> >             System.out.println("Value of constant: " + MySysProps.CONST);
> >       }
> > }
> >
> > Output:
> >
> > C:\>java SysPropsTest
> > Value of constant: default
> >
> > // no need to recompile, just run with a new value:
> >
> > C:\>java -Dmyprop=foobar SysPropsTest
> > Value of constant: foobar
> >
> > --
> > Colin Sharples
> >
> >
> > CAUTION - This message may contain privileged and confidential information 
> > intended only for the use of the addressee named above. If you are not the 
> > intended recipient of this message you are hereby notified that any use, 
> > dissemination, distribution or reproduction of this message is prohibited. 
> > If you have received this message in error please notify Bank of New 
> > Zealand immediately. Any views expressed in this message are those of the 
> > individual sender and may not necessarily reflect the views of Bank of New 
> > Zealand.
> >
>

Reply via email to