because you are a Java programmer you know that even a final variable can still be manipulated unless it's immutable.
How it is possible?
If an instance variable of servlet is final int i = 9; then it's not thread safe, I agree, but it's not harmful because no thread can modify this variable....
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.
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.
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.