This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The sentence you quoted confuses between immutable and final.
I assume by manipulation a change in state or behaviour is intended.
Final when applied to a variable implies that variable reference can't be changed. Remember that final can be applied to a class and variable. When final is applied to a class that class cannot be subclassed.
Immutable is used in conjuction with objects. It is not used in conjuction with variables or classes. Immutable means the object once it is constructed cannot be changed.
The object referenced by the final variable can still be manipulated unless the object the variable references is immutable.
Marking a variable final is one of those things in Java that is a little counter-intuitive if you ask me. The trick, for beginners especially, is to keep in mind that final means completely different things when applied to primitives, objects, and methods.
If I mark an int as final, no one can change the integer. But if I mark an object as final, the object can still be changed, just not my specific pointer to that object. If you want to make an object truly final, you have to make sure there are no settings or exposed variables that can change the object in any way.
The trick, for beginners especially, is to keep in mind that final means completely different things when applied to primitives, objects ...
And the test to achieve the next level is to realize that they are not completely different but exactly the same. Take the pebble from my hand, grashopper.
The variable holds either a primitive value or a reference to an object. You cannot change what the variable holds. That part is the same. You can change parts of the object to which the reference points (if the object is not immutable) but you cannot change anything about the primitive. That part is different. So "same" and "different" are both correct answers if you frame them just right.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Oct 11, 2004
Originally posted by Jim Yingst: [Rathi]: If an instance variable of servlet is final int i = 9; then it's not thread safe, I agree...
It certainly is thread safe. Why wouldn't it be? Final primitives are thread safe, and final references to immutable reference types are thread safe.
what is thread safe?
If two threads are accessing something simultaneously then it is not thread safe.
If two threads are changing (modifying) or can change something simultaneously then it is not thread safe.
The key phrase is "unwanted interaction". These can cause inconsistencies in the calculations being performed in one or more threads that are accessing the shared resource. If there is no possibility that a resource can be changed by other threads, such as in the cases that Jim cited, then the resource is said to be thread safe.