VAJ is holding on to static final definitions!_NEWLINE_!!_NEWLINE_!I have...
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   R_Christie
Posted On:   Thursday, February 15, 2001 03:36 AM

VAJ is holding on to static final definitions I have a simple Interface A, which has a single definition in it : public interface A { public static final String A_A = "Hello"; } I then have a simple class B which implements A, thus: public class B implements A { private static final String B_A = A_A; private static String B_B; } If I create an instance of class B, and display the contents of both B_A and B_B I (not surpisingly)get the same value ("Hello") (here's my main() method that does this). public static void main(java.lang.String[] arg   More>>

VAJ is holding on to static final definitions


I have a simple Interface A, which has a single definition in it :



public interface A
{
public static final String A_A = "Hello";
}



I then have a simple class B which implements A, thus:



public class B implements A
{
private static final String B_A = A_A;
private static String B_B;
}



If I create an instance of class B, and display the contents of both B_A and B_B I (not surpisingly)get the same value ("Hello") (here's my main() method that does this).



public static void main(java.lang.String[] args)
{
// Insert code to start the application here.
B b = new B();
System.out.println(b.toString());
try
{
int C = System.in.read();
}
catch (Exception e)
{
}
}



Now, if I don't terminate this invocation, and go back and alter the value of the definition of A_A in Interface A, and run B.main() again, B_A displays a "Hello", and B_B as th new value.


I can understand what is going on here. But now, if I terminate both these executables, and run again, B_A and B_B still how up as diferent values. And to further my frustration, if I close down VAJ, and re-run, B_A still has the old value, whereas B_B has the new value.


VAJ is obviously holding on to the static final instance. How is it doing it, where is i holding the reference, and how do I force re-initialisation of the final statics ?

   <<Less

Re: VAJ is holding on to static final definitions!_NEWLINE_!!_NEWLINE_!I have...

Posted By:   Eric_Rizzo  
Posted On:   Thursday, November 29, 2001 10:44 AM

This is behaving as designed, although it is a little confusing. The key to understanding it is to rememeber that constants (static final variables) are inlined by the compiler. In your example, that means that there is no reference to A_A in the bytecode of B. Instead, A-A's value at the time B was compiled is "hard-coded" into the byecode that uses B_A. So changing A_A does not affect B_A.

The way to force B_A to get the new value is to force B to be recompiled (see Is there a way to force VAJ 3.5 to re-compile w/o modifying the class?).


Incidentally, the same situation occurrs with the JDK compiler also - as I said, its not a bug, just a side effect of compiler optimizations for constant variables.

About | Sitemap | Contact