Edward A. Falk said:
(snip, someone wrote)
(then I wrote)
At least as of Java 6 (aka 1.6, JLS 3) since about 4 years ago:
You can assign a nonstatic final field once in a ctor, counting a
class-scope {code} block as prepended to all ctors, or initialize in
the declaration, but not both.
Similarly you can assign a static final field once in a static{code}
block or initialize in the declaration but not both.
And you can assign a final local variable (necessarily nonstatic) once
in the code or initialize in the declaration but not both.
Aside: Java uses 'member' to include fields, methods, and nested
classes or interfaces. Of these only fields are variables; methods and
nested classes can also be marked final but with different meaning.
You are getting pretty close to the Java version of the C question
that started the thread. That is, the difference between a pointer
and what it points to.
A static final variable of a primitive type (char, byte, short, int,
long, float, double) can't be changed and, last I knew, had to be
initialized on its declaration.
See above. Once initialized it can't be assigned again.
However, a static final field of primitive or String type, which is
initialized in the declaration by a constant expression, can be
compiled as a constant into another classes that uses it, instead of
an access to the actual field, and thus not pick-up changes without
recompile. <ObAlmostC>This is similar to C++'s but not C's special
treatment of a const variable of integer type initialized by a
constant expression as allowed in a(nother) constant expression, such
as a bound in an array declaration or a switch case label. said:
A static final Object, should mean that you can't change the Object
reference variable to refer to another Object, but you could still
change instance variables of the Object, if otherwise allowed.
Well, if a final variable (local or field, or parameter) points to an
actual java.lang.Object, that type has no fields you can access and
the only state you can affect is the notify list which AFAIK cannot be
observed except through the debugger interface. But Object is the top
of the inheritance hierarchy so such a variable can actually point to
an object of any type (including an array, since arrays in Java are
objects of synthesized types) and if that type has mutable fields
and/or state (and most do) yes you can mutate it.