aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final modifier 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final modifier" Watch "final modifier" New topic
Author

final modifier

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383


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?


SCJP-1.5<br />SCWCD-1.4
Michael Raymond Jr.
Ranch Hand

Joined: May 16, 2005
Posts: 178
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.



Scooby Snacks for everyone...<br /> <br />SCJA, SCJP 1.4
Michael Raymond Jr.
Ranch Hand

Joined: May 16, 2005
Posts: 178
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.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961

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

shubhra chauhan
Greenhorn

Joined: Aug 31, 2007
Posts: 21
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: final modifier