BINARY COMPATIBILITY
final
Fields and Constants
13.4.8
early implementations of Java this example would run without error, because of a
flaw in the implementation.)
We call a field that is
static
,
final
, and initialized with a compile time con
stant expression a
primitive constant
. Note that all fields in interfaces are implic
itly
static
and
final
, and they are often, but not always, constants.
If a field is not a primitive constant, then deleting the keyword
final
or
changing the value to which the field is initialized does not break compatibility
with existing binaries.
If a field is a primitive constant, 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 constant unless they are
recompiled. If the example:
class Flags { final static boolean debug = true; }
class Test {
public static void main(String[] args) {
if (Flags.debug)
System.out.println("debug is true");
}
}
is compiled and executed, it produces the output:
debug is true
Suppose that a new version of class
Flags
is produced:
class Flags { final static boolean debug = false; }
If
Flags
is recompiled but not
Test
, then running the new binary with the exist
ing binary of
Test
produces the output:
debug is true
because the value of
debug
was a compile time primitive constant, and could have
been used in compiling
Test
without making a reference to the class
Flags
.
This result is a side effect of the decision to support conditional compilation,
as discussed at the end of 14.19.
This behavior would not change if
Flags
were changed to be an interface, as
in the modified example:
interface Flags { boolean debug = true; }
class Test {
251
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved