Java Reference
In-Depth Information
public static final int N = ...;
There is no problem with:
public static int N = ...;
if
N
need not be read-only. We also recommend, as a general rule, that only truly constant
values be declared in interfaces.
We note, but do not recommend, that if a field of primitive type of an interface may change,
its value may be expressed idiomatically as in:
interface Flags {
boolean debug = new Boolean(true).booleanValue();
}
ensuring that this value is not a constant. Similar idioms exist for the other primitive types.
One other thing to note is that
static final
fields that have constant values (whether of primit-
ive or
String
type) must never appear to have the default initial value for their type (§
4.12.5
)
.
This means that all such fields appear to be initialized first during class initialization
13.4.10.
static
Fields
If a field that is not declared
private
was not declared
static
and is changed to be declared
stat-
ic
, or vice versa, then a linkage error, specifically an
IncompatibleClassChangeError
, will result
if the field is used by a pre-existing binary which expected a field of the other kind. Such
changes are not recommended in code that has been widely distributed.
13.4.11.
transient
Fields
Adding or deleting a
transient
modifier of a field does not break compatibility with pre-ex-
isting binaries.
13.4.12. Method and Constructor Declarations
Adding a method or constructor declaration to a class will not break compatibility with any
pre-existing binaries, even in the case where a type could no longer be recompiled because
an invocation previously referenced a method or constructor of a superclass with an incom-
patible type. The previously compiled class with such a reference will continue to reference
the method or constructor declared in a superclass.
Assume a reference to a method
m
with qualifying type
T
. Assume further that
m
is in fact
an instance (respectively
static
) method declared in a superclass of
T
,
S
.