Java Reference
In-Depth Information
}
}
In this solution, the private version value cannot be copied into the
Bar
class when it
is compiled, consequently preventing the bug. Note that this transformation imposes little
or no performance penalty because most just-in-time (JIT) code generators can inline the
getVersion()
method at runtime.
Applicability
Declaring a value that changes over the lifetime of the software as
final
may lead to un-
expected results.
According to §9.3, “Field (Constant) Declarations,” of the JLS [JLS 2013], “Every
field declaration in the body of an interface is implicitly
public
,
static
, and
final
. It
is permitted to redundantly specify any or all of these modifiers for such fields.” There-
fore, this guideline does not apply to fields defined in interfaces. Clearly, if the value of a
field in an interface changes, every class that implements or uses the interface must be re-
compiled.See
Guideline35
,
“
Carefullydesigninterfacesbeforereleasingthem
,
”formore
information.
Constants declared using the
enum
type are permitted to violate this guideline.
Constants whose value never changes throughout the entire lifetime of the software
maybedeclaredas
final
.Forexample,theJLSrecommendsthatmathematicalconstants
be declared
final
.
Bibliography
[JLS 2013]
§4.12.4, “
final
Variables”
§8.3.1.1, “
static
Fields”
§9.3, “Field (Constant) Declarations”
§13.4.9, “
final
Fields and Constants”
32. Avoid cyclic dependencies between packages
Both
The Elements of Java
™
Style
[Allen2000]andtheJPLJavaCodingStandard[Have-
lund 2009] require that the dependency structure of a package must never contain cycles;
that is, it must be representable as a directed acyclic graph (DAG).