This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "explain this code" Watch "explain this code" New topic
Author

explain this code

anamika henry
Ranch Hand

Joined: Mar 09, 2007
Posts: 30
can any one please explain me the line 6,7,9 and 11

1.class Fizz{
2.int x=5;
3. public static void main(String args[]){
4. final Fizz f1= new Fizz();
5.Fizz f2 =new Fizz();
6. Fizz f3 = new FizzSwitch(f1,f2);
7. System.out.println((f1==f3) + " " + (f1.x==f3.x));
8.}
9.static Fizz FizzSwitch(Fizz x,Fizz Y){
10. final Fizz z = x;
11.z.x=6;
12. return z;
13.}
14.}
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Modified Code:



Now the output is true true.

can any one please explain me the line 6,7,9 and 11


Line 6: You are calling a static method FizzSwitch() passing f1 and f2 to it.

Line 9: Nothing special here, simple static method signature. If you are
getting confused with final parameter, no problem, make then both final,
it will be accepted but inside the method you can't dereference them.

Line 10: Another reference variable z of type Fizz is created that is final,
means can't be dereferenced once referenced. You set its reference to the x,
that is f1 in main;

Line 11: You set the value of x to 6 hence references can be final, objects
can't be. You can change the internal state of an object as you did by changing the value of x to 6.
You returned the z;

Line 7: Now what is returned from FizzSwitch() method, you assigned to f3.
Now you know f3 and f1 are referring to same object on the heap.
They are == (referring to same object) and their value x is ==.



Regards,
cmbhatt


cmbhatt
Rupali Deshpande
Greenhorn

Joined: Apr 16, 2006
Posts: 5
Hi Chandra,

I am unable to compile the code



In my eclipse its showing some problem at line 6

-------------
Rupali
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
What is that error Rupali?
Copy paste here!!




Regards,
cmbhatt
Rupali Deshpande
Greenhorn

Joined: Apr 16, 2006
Posts: 5
Hi

The error is
"FizzSwitch cannot be resolved to a type" at line 6
lalitha kalanadhbhatla
Greenhorn

Joined: Nov 05, 2006
Posts: 7
Hi,
Here the Z is declared as final type and it is refernced to X=5 and in the next line it is given Z.X=6.Does it allow the final varibles to be reassgined a value after declaring it final???
actually tell me the exact o/p of the program.Its bit confusing.
Regards,
Lalitha.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Rupali,

You mistake is:
Fizz f3 = new FizzSwitch(f1,f2);

remove "new" from that line! FizzSwitch is not a class, it is a static method.

Original post had this error, i modified that code, then answered.


Did you get that?



Regards,
cmbhatt
Rupali Deshpande
Greenhorn

Joined: Apr 16, 2006
Posts: 5
Hey,
thanks chandra.
It was really a silly mistake.



-------------

Rupali
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: explain this code
 
Similar Threads
doubt in chapter 3 k&B book
chapter 3 self test question 5, K&B
Final
explain this code
help me in understanding this program