Java Reference
In-Depth Information
In particular, no linkage error will occur in the case where a class could no longer be re-
compiled because a field access previously referenced a field of a superclass with an in-
compatible type. The previously compiled class with such a reference will continue to ref-
erence the field declared in a superclass.
Example 13.4.8-1. Adding A Field Declaration
class Hyper { String h = "hyper"; }
class Super extends Hyper { String s = "super"; }
class Test {
public static void main(String[] args) {
System.out.println(new Super().h);
}
}
This program produces the output:
hyper
Suppose a new version of class
Super
is produced:
class Super extends Hyper {
String s = "super";
int h = 0;
}
Then, recompiling
Hyper
and
Super
, and executing the resulting new binaries with the
old binary of
Test
produces the output:
hyper
The field
h
of
Hyper
is output by the original binary of
Test
. While this may seem sur-
prising at first, it serves to reduce the number of incompatibilities that occur at run
time. (In an ideal world, all source files that needed recompilation would be recom-
piled whenever any one of them changed, eliminating such surprises. But such a mass
recompilation is often impractical or impossible, especially in the Internet. And, as
was previously noted, such recompilation would sometimes require further changes
to the source code.)
As another example, if the program:
class Hyper { String h = "Hyper"; }
class Super extends Hyper { }