Java Reference
In-Depth Information
13.4.9. final Fields and Constants
If a field that was not declared final is changed to be declared final , then it can break com-
patibility with pre-existing binaries that attempt to assign new values to the field.
Example 13.4.9-1. Changing A Variable To Be final
Click here to view code image
class Super { static char s; }
class Test extends Super {
public static void main(String[] args) {
s = 'a';
System.out.println(s);
}
}
This program produces the output:
a
Suppose that a new version of class Super is produced:
class Super { static final char s = 'b'; }
If Super is recompiled but not Test , then running the new binary with the existing binary
of Test results in a IllegalAccessError .
Deleting the keyword final or changing the value to which a field is initialized does not
break compatibility with existing binaries.
If a field is a constant variable (§ 4.12.4 ), then deleting the keyword final or changing its
value will not break compatibility with pre-existing binaries by causing them not to run,
but they will not see any new value for the usage of the field unless they are recompiled.
This is true even if the usage itself is not a compile-time constant expression (§ 15.28 ) .
This result is a side-effect of the decision to support conditional compilation, as discussed
at the end of § 14.21 .
Example 13.4.9-2. Conditional Compilation
If the example:
Click here to view code image
class Flags { static final boolean debug = true; }
class Test {
Search WWH ::




Custom Search