• 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

final modifier

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code is from chapter-3, K & B book,page:262.
It is describe in the answer that "the references f1,z & f3 all refer to the same instance of Fizz. AS I understand they are talking about variable x. Please correct me if I am wrong.
They also describe.
"The final modifier assures that a reference variable cannot be referred to a different object, but final doesn't keep the object's state from changing."
What they want to say by this description? How it is related to this question?

In short I don't understand this example at all. Can anyone please help me?
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:


This code is from chapter-3, K & B book,page:262.
It is describe in the answer that "the references f1,z & f3 all refer to the same instance of Fizz. AS I understand they are talking about variable x. Please correct me if I am wrong.
They also describe.
"The final modifier assures that a reference variable cannot be referred to a different object, but final doesn't keep the object's state from changing."
What they want to say by this description? How it is related to this question?

In short I don't understand this example at all. Can anyone please help me?



looks to me that:

System.out.println((f1==f3

f1==f3 is boolean, so there's no assignment. i think all this code is legal.


z.x=6 is updating an instance variable, not changing z's reference to another object, which would be illegal since z is final.



"The final modifier assures that a reference variable cannot be referred to a different object, but final doesn't keep the object's state from changing."

so that says when the first Fizz object (f1) is createdm, its instance variable (x) is 5. later, antoher Fizz object is created and it changes x to 6. so the quotes are saying you can change the instance variable of an objects state, just not change the objects reference to refer to a new object because it's final.




than's what i think... i'm studying as you are.

 
Michael Raymond Jr.
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Raymond Jr.:


looks to me that:

System.out.println((f1==f3

f1==f3 is boolean, so there's no assignment. i think all this code is legal.


z.x=6 is updating an instance variable, not changing z's reference to another object, which would be illegal since z is final.



"The final modifier assures that a reference variable cannot be referred to a different object, but final doesn't keep the object's state from changing."

so that says when the first Fizz object (f1) is createdm, its instance variable (x) is 5. later, antoher Fizz object is created and it changes x to 6. so the quotes are saying you can change the instance variable of an objects state, just not change the objects reference to refer to a new object because it's final.




than's what i think... i'm studying as you are.




I take that back about a new object being created in the fizzSwitch() nmethod. z is referring to the same object as f1 is. still nothing changes, as the instance variables are not final and even though the objects are, they can still update instance variables as per the quoted text from k/b book you refer to.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"The final modifier assures that a reference variable cannot be referred to a different object, but final doesn't keep the object's state from changing."
What they want to say by this description? How it is related to this question?



It means that altought you cannot change the reference of a variable declared final you can still change the state of the object.

Example


In your code, in the method Fizz FizzSwitch(Fizz x,Fizz y) is altering the value of the field x, altough a reference variable is declared final.

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi dolly..

see x as a state of an object belongs to f1,f2 & f3 also.

now in this line of code

Fizz f3=FizzSwitch(f1,f2); //1
static Fizz FizzSwitch(Fizz x,Fizz y){//2
final Fizz z=x; //3
z.x=6;
return z; }}

now f1 is assigned to z(see line 1, 2, 3), this means now z and f1 refers to the same object which has x.
then z.x=6 changes the value of x from 5 to 6. this value is now effective for f1 and f3 only because of returning z. f1,f3,z refers to the same object having the same value ie x=6. only f2 has x=5.

if f1 was not final it would have a different reference value means other than earlier one but on contrary of that its refrence value is still the same but its object state is only modified.

i tried to make it as simple as psbl...hp it'll help you..
[ September 14, 2007: Message edited by: shubhra chauhan ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic