Originally posted by koray guclu:
[...] and another thread will run try to change this varianle str [...]
It can't. Strings are immutable.
You may misunderstand the meaning of "final". When I say "final FooBar foobar", I'm creating a reference called "foobar" to a FooBar object. The reference is final and cannot be changed to point to a different FooBar object. The object it refers to, however,
can still be changed unless it is of an immutable class such as String.
When you "change a String", in reality you are creating a wholly new String object and assigning a reference to the variable. In your code, myfunction() would still keep on using its reference to the old String so there's no problem at all. You can omit the "final" modifier, it has no impact on the issue.
If you have a mutable object and you want to prevent everyone else from changing it, "final" won't help you either. You'll have to create your own copy of the object. One of the most efficient ways to do this is to make your object Cloneable and implement clone().
- Peter
[This message has been edited by Peter den Haan (edited November 08, 2001).]