File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes chapter 3 self test question 5, K&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "chapter 3 self test question 5, K&B" Watch "chapter 3 self test question 5, K&B" New topic
Author

chapter 3 self test question 5, K&B

Mauricio Archbold Babroza
Greenhorn

Joined: Apr 01, 2009
Posts: 11

why the answer is true true???
if the code the f1 and z does not have final the answer is the same???
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Mauricio Archbold Babroza wrote:
why the answer is true true???
if the code the f1 and z does not have final the answer is the same???


check carefully the FizzSwitch(Fizz x, Fizz y) method.


It assigns the first Fizz argument to a final Fizz z. Then you change the x variable, and return the same Fizz object, except that now Fizz.x = 6
Yes, you can change the object variables even if the object is declared final. Only thing you can't do with a final object is to re-assign it. For example, you cannot write


In effect you are passing f1 and f2 to fizzSwitch() method and getting back f1 (with f1.x = 6). The result is assigned to f3.
f1 and f3 will be the same because both f1 and f3 are references to the same object. Changes in either f1 or f3 will reflect each other because of this.


Eve if you take out 'final', result will be same. I think the final is added to create more confusion!


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
You can change the state of a a final object but you cannot make the reference start referring to a new a object.

Best Regards,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: chapter 3 self test question 5, K&B