...
In this noncompliant code example, class Foo
declares a field whose value represents the version of the software. The field is subsequently accessed by class Bar
from a separate compilation unit.
Foo.java
:
Code Block | ||
---|---|---|
| ||
class Foo { static public final int VERSION = 1; // ... } |
Bar.java
:
Code Block | ||
---|---|---|
| ||
class Bar { public static void main(String[] args) { System.out.println("You are using version " + Foo.VERSION); } } |
...
Code Block | ||
---|---|---|
| ||
class Bar { public static void main(String[] args) { System.out.println("You are using version " + Foo.getVersion()); } } |
The As a result, the private version value can therefore not cannot be copied into the Bar
class when it is compiled, thus consequently preventing the bug. Note that most JITs are capable of inlining the getVersion()
method at runtime; consequently there is little or no performance penalty incurred.
...