Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how final variable can be manupulated?

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although this is not at all related to servlet question but I found this in HFS&J:

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....

Please comments.

Thanks.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post these in the correct forum. I'm moving this to the Java in General (beginner) forum.

Dave
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are they talking about object manupulation?

final Dog d = new Dog();

Here this Dog object can be manupulatable but d can't refer to any other Dog object...
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Truth is

The object referenced by the final variable can still be manipulated unless the object the variable references is immutable.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

or

If two threads are changing (modifying) or can change something simultaneously then it is not thread safe.

Please comments...

Thanks.
 
Junilu Lacar
Bartender
Pie
Posts: 7316
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a simple definition of thread safety: http://whatis.techtarget.com/definition/0,,sid9_gci331590,00.html

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.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the second one is right...


If two threads are changing (modifying) or can change something simultaneously then it is not thread safe.


then certainly, *final int i = 9;* is thread safe....

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic