• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how final variable can be manupulated?

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic