You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Tarun Garg <ta...@induslogic.com> on 2001/09/28 08:17:11 UTC

static final string.

This probably is not exactly an ant issue, but some of you might have faced
it that's why I am posting it here.
I have two classes. A and B.
class A has a member in it which is declared something like:

public static final String mesg="hello";

Now class B uses this string for some purpose of its own.
Lets say it just prints the message.
class B {
public static void main(String args[])
    {
        System.out.println(A.mesg);
    }
}

Now when I change the mesg in class A to "hello world", Ant recompiles only
the class A as A.java is the only file that has been modified.
Now when I run class B, the message still remains "hello".
I believe this has something to do the way compilers are implemented. I
tried both javac and jikes. Both exhibit the same behaviour.
During development it can happen that you need to change your final
variables too.
Has anybody faced this problem before ? How do you solve it ?
One solution would be that whenever somebody changes a final variable, she
informs everybody working on that project to do a clean build.
But that's not a very clean solution.
Any tips ?



Re: static final string.

Posted by Peter Donald <do...@apache.org>.
On Fri, 28 Sep 2001 17:08, Erik Hatcher wrote:
> Have a look at the optional <depend> task.  I think it will solve this
> particular issue.

Unfortunately because Strings are treated the same as primitive objects they 
are inlined when they are static final values and thus the depends task wont 
help there ;(

-- 
Cheers,

Pete

---------------------------------------------------
"It is easy to dodge our responsibilities, but we 
cannot dodge the consequences of dodging our 
responsibilities." -Josiah Stamp 
---------------------------------------------------

Re: static final string.

Posted by Erik Hatcher <ja...@ehatchersolutions.com>.
Have a look at the optional <depend> task.  I think it will solve this
particular issue.

    Erik


----- Original Message -----
From: "Tarun Garg" <ta...@induslogic.com>
To: "ant" <an...@jakarta.apache.org>
Sent: Thursday, September 27, 2001 11:17 PM
Subject: static final string.


> This probably is not exactly an ant issue, but some of you might have
faced
> it that's why I am posting it here.
> I have two classes. A and B.
> class A has a member in it which is declared something like:
>
> public static final String mesg="hello";
>
> Now class B uses this string for some purpose of its own.
> Lets say it just prints the message.
> class B {
> public static void main(String args[])
>     {
>         System.out.println(A.mesg);
>     }
> }
>
> Now when I change the mesg in class A to "hello world", Ant recompiles
only
> the class A as A.java is the only file that has been modified.
> Now when I run class B, the message still remains "hello".
> I believe this has something to do the way compilers are implemented. I
> tried both javac and jikes. Both exhibit the same behaviour.
> During development it can happen that you need to change your final
> variables too.
> Has anybody faced this problem before ? How do you solve it ?
> One solution would be that whenever somebody changes a final variable, she
> informs everybody working on that project to do a clean build.
> But that's not a very clean solution.
> Any tips ?
>
>
>


Re: static final string.

Posted by Peter Donald <do...@apache.org>.
On Fri, 28 Sep 2001 16:17, Tarun Garg wrote:
> public static final String mesg="hello";
>
> Now class B uses this string for some purpose of its own.
> Lets say it just prints the message.
> class B {
> public static void main(String args[])
>     {
>         System.out.println(A.mesg);
>     }
> }

Theres no clean way to do it as it will get inlined in other class. You could 
wrap the constant in another object (because the other object wont be 
inlined). You could also put in ugly hacks like one of

class B
{
  private static Class CLASS_HACK_REF = A.class;
}

or perhaps make it runtime final like

class A
{
  public static final String mesg = 
    System.getProperty( "some.key.that.not.exist", "hello" );
}

or just do clean builds all the times when you notice such changes.

-- 
Cheers,

Pete

----------------------------------------------------------------
Fools ignore complexity.  Pragmatists suffer it.
Some can avoid it.  Geniuses remove it.
-- Perlis's Programming Proverb #58, SIGPLAN Notices, Sept. 1982
----------------------------------------------------------------

Re: static final string.

Posted by Jim Cheesman <jc...@msl.es>.
At 08:45 AM 28/09/01, you wrote:
> > Now when I change the mesg in class A to "hello world", Ant recompiles
>only
> > the class A as A.java is the only file that has been modified.
> > Now when I run class B, the message still remains "hello".
> > I believe this has something to do the way compilers are implemented. I
> > tried both javac and jikes. Both exhibit the same behaviour.
> > During development it can happen that you need to change your final
> > variables too.
> > Has anybody faced this problem before ? How do you solve it ?
> > One solution would be that whenever somebody changes a final variable, she
> > informs everybody working on that project to do a clean build.
> > But that's not a very clean solution.
> > Any tips ?



Have a seperate machine do full clean builds from scratch? Helps sort out 
classpath, dependency issues too... Use the CVS/VSS tasks, run your tests...



Other than that, I can't think of anything.



Jim






--

                           *   Jim Cheesman   *
             Trabajo: 
jchees@msl.es - (34)(91) 724 9200 x 2360
            As easy as 
3.1415926535897932384626433832795028841



Re: static final string.

Posted by Tarun Garg <ta...@induslogic.com>.
The java language specification(13.4.8) answers my question.
Maybe somebody else can gain from it too.
I reproduce the following from the specs.
.....
The best way to avoid problems with "inconstant constants" in
widely-distributed code is to declare as compile time constants only values
which truly are unlikely ever to change. Many compile time constants in
interfaces are small integer values replacing enumerated types, which the
language does not support; these small values can be chosen arbitrarily, and
should not need to be changed. Other than for true mathematical constants,
we recommend that source code make very sparing use of class variables that
are declared static and final. If the read-only nature of final is required,
a better choice is to declare a private static variable and a suitable
accessor method to get its value. Thus we recommend:


private static int N;
public static int getN() { return N; }

rather than:

public static final int N = ...;

...........

----- Original Message -----
From: "Tarun Garg" <ta...@induslogic.com>
To: <an...@jakarta.apache.org>
Sent: Friday, September 28, 2001 12:15 PM
Subject: Re: static final string.


> The java language itself specifies this behaviour.
> So I guess nothing much can be done about this.
>
> ----- Original Message -----
> From: "Tarun Garg" <ta...@induslogic.com>
> To: "ant" <an...@jakarta.apache.org>
> Sent: Friday, September 28, 2001 11:47 AM
> Subject: static final string.
>
>
> > This probably is not exactly an ant issue, but some of you might have
> faced
> > it that's why I am posting it here.
> > I have two classes. A and B.
> > class A has a member in it which is declared something like:
> >
> > public static final String mesg="hello";
> >
> > Now class B uses this string for some purpose of its own.
> > Lets say it just prints the message.
> > class B {
> > public static void main(String args[])
> >     {
> >         System.out.println(A.mesg);
> >     }
> > }
> >
> > Now when I change the mesg in class A to "hello world", Ant recompiles
> only
> > the class A as A.java is the only file that has been modified.
> > Now when I run class B, the message still remains "hello".
> > I believe this has something to do the way compilers are implemented. I
> > tried both javac and jikes. Both exhibit the same behaviour.
> > During development it can happen that you need to change your final
> > variables too.
> > Has anybody faced this problem before ? How do you solve it ?
> > One solution would be that whenever somebody changes a final variable,
she
> > informs everybody working on that project to do a clean build.
> > But that's not a very clean solution.
> > Any tips ?
> >
> >
>


Re: static final string.

Posted by Tarun Garg <ta...@induslogic.com>.
The java language itself specifies this behaviour.
So I guess nothing much can be done about this.

----- Original Message -----
From: "Tarun Garg" <ta...@induslogic.com>
To: "ant" <an...@jakarta.apache.org>
Sent: Friday, September 28, 2001 11:47 AM
Subject: static final string.


> This probably is not exactly an ant issue, but some of you might have
faced
> it that's why I am posting it here.
> I have two classes. A and B.
> class A has a member in it which is declared something like:
>
> public static final String mesg="hello";
>
> Now class B uses this string for some purpose of its own.
> Lets say it just prints the message.
> class B {
> public static void main(String args[])
>     {
>         System.out.println(A.mesg);
>     }
> }
>
> Now when I change the mesg in class A to "hello world", Ant recompiles
only
> the class A as A.java is the only file that has been modified.
> Now when I run class B, the message still remains "hello".
> I believe this has something to do the way compilers are implemented. I
> tried both javac and jikes. Both exhibit the same behaviour.
> During development it can happen that you need to change your final
> variables too.
> Has anybody faced this problem before ? How do you solve it ?
> One solution would be that whenever somebody changes a final variable, she
> informs everybody working on that project to do a clean build.
> But that's not a very clean solution.
> Any tips ?
>
>