Harry Bosch said:
Like I said in my post, I wasn't really sure about the volitile issue.
So, again, if the data member is public, and is going to be used in a
multi-threaded environment, synchonize the method, the object, the
class, whatever, and call it a day.
As for immutability...
java.lang.Double objects are Immutable.
double values are mutable.
Is the double *VALUE* itself mutable (e.g. the value of 2.0 can change),
or is it actually the variable containing the double value mutable (i.e. the
variable foo, which happens to contain the value 2.0 for now, can change)?
This is, of course, just semantic nitpicking, but I get the feeling that's
what the theme of this branch of this thread is all about anyway.
Making a variable immutable has nothing to do with the 'final'
operator.
It depends on your definition of "mutable" and "immutable", of course.
Under Roedy's definition ("mutable" means "can change" and "immutable" means
"cannot change"), then the "final" operator makes the reference it's
operating immutable; whether the object that reference points to is
immutable or not is an entirely different story!
My definition says that "mutable" and "immutable" only make sense for
reference types (i.e. not primitive types), and I don't use the term for
variables themselves. That is, if a variable can only point to one object, I
say that the variable is "final" or "constant", but not that it is
"immutable". But that's just my terminology; you're free to use your own as
long as when we have a discussion, such differences are made clear to avoid
misunderstandings.
A final operator means that an variable's reference cannot
be changed after it has been initialized. This does not mean that the
object is immutable.
I think everyone agrees with the above. They just disagree on when and
where the terms "mutable" and "immutable" are appropriate.
Immutability basically means that the object's data cannot change
without changing it's reference.
I don't require such a tight restriction before saying an object is
immutable, but I agree with the general concept here. I think though, if an
object's internal (private) data changes, but such changes are not ever
visible outside, then the object is still considered immutable.
[ snip the rest, because I think the points presented there were not under
dispute ]
- Oliver