In java, say I have the following
==fileA.java==
class A
{
public static final int SIZE = 100;
}
Then in another file i use this value
==fileB.java==
import A;
class b
{
Object[] temp = new Object[A.SIZE];
}
When this gets compiled does SIZE
get replaced with the value 100, so that if i were to down the road replace the FileA.jar but not FileB.jar would the object array get the new value or would it have been hardcoded to 100 because thats the value when it was originally built?
Thanks,
Stephanie
Java does optimise these sorts of values but only if they are in the same class. In this case the JVM looks in A.SIZE rather than optimizing it because of the usage case you are considering.
As an optimization the compiler will inline that final variable.
So at compile time it will look like.
One thing should note is: static final value is known at compile time if the value is not known at compile time, compiler won't replaces the constant name everywhere in the code with its value.
first compile TestA and TestB, run TestB
then change TestA.getValue() to return 200, compile TestA, run TestB, TestB will get the new value enter image description here
You can keep the constant from being compiled into B, by doing