File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes explain this code 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 "explain this code" Watch "explain this code" New topic

explain this code

krithika naya

Joined: Oct 25, 2007
Posts: 10
class Fizz {
int x = 5;
public static void main(String[] args) {
final Fizz f1 = new Fizz();
Fizz f2 = new Fizz();
Fizz f3 = FizzSwitch(f1,f2);
System.out.println((f1 == f3) + " " + (f1.x == f3.x));
static Fizz FizzSwitch(Fizz x, Fizz y) {
final Fizz z = x;
z.x = 6;
return z;
} }
What is the result?
A. true true
B. false true
C. true false
D. false false
E. Compilation fails.
F. An exception is thrown at runtime.

� A is correct. The references f1, z, and f3 all refer to the same instance of Fizz. 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.
�˚ B, C, D, E, and F are incorrect based on the above.

here what does the object state referred to?

z.x=6; explain this line

And explain this code.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

Karthiga, please quote your sources.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
krithika naya

Joined: Oct 25, 2007
Posts: 10
sun certified programmer for java 5 by sierra and Bates.

I got this from 3rd chapter,self test question .
Joshua Mark

Joined: Sep 23, 2007
Posts: 15
When FizzSwitch is called, it is accepting Fizz f1 and Fizz f2 as arguments....where Fizz x == f1 and Fizz y == f2

then Fizz z = x means Fizz z = x = f1 which means the reference variables z, x and f1 all point to the same object.

then z.x means set member data x(because the Fizz object has an attribute x - yes this is confusing) on object z to 6. Since z, x, and f1 all reference the same object when you set z.x to 6, x.x = 6 and f1.x = 6.

then it returns the Fizz z and assigns it to Fizz f3. Since z, x and f1 all referenced the same object, f3 now also references the same object, so f3 and f1 both point to the same object. Since the references are both pointing to the same object f1 == f3 is true. And since that is true, obviously f1.x = f3.x

If you are getting confused by line 4 (final Fizz f1 = new Fizz()), all this means is that the reference variable f1 can't be changed. The member data within the Fizz f1 object can change, but you can't assign a different object to f1. Ie if you were to insert f1 = f2 after line five, you would get an exception. Same thing within the FizzSwitch method, line 10 (final Fizz z = x), this means that z can never reference any other object except x, which happens to also equal f1.

Hope that helps.
krithika naya

Joined: Oct 25, 2007
Posts: 10
Thanks you very much. now i got it.
I agree. Here's the link:
subject: explain this code
It's not a secret anymore!