aspose file tools*
The moose likes Beginning Java and the fly likes how final variable can be manupulated? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how final variable can be manupulated?" Watch "how final variable can be manupulated?" New topic
Author

how final variable can be manupulated?

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Mar 06, 2001
Posts: 13459

Please post these in the correct forum. I'm moving this to the Java in General (beginner) forum.

Dave
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Oct 12, 2004
Posts: 306
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

Joined: Oct 23, 2005
Posts: 3710
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Feb 26, 2001
Posts: 4714
    
    7

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how final variable can be manupulated?