Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
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
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

explain this code

Ranch Hand
Posts: 30
• Number of slices to send:
Optional 'thank-you' note:
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.}

Ranch Hand
Posts: 1710
• Number of slices to send:
Optional 'thank-you' note:
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

Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
Hi Chandra,

I am unable to compile the code

In my eclipse its showing some problem at line 6

-------------
Rupali

Chandra Bhatt
Ranch Hand
Posts: 1710
• Number of slices to send:
Optional 'thank-you' note:
What is that error Rupali?
Copy paste here!!

Regards,
cmbhatt

Rupali Deshpande
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
Hi

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

Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 1710
• Number of slices to send:
Optional 'thank-you' note:
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.

Did you get that?

Regards,
cmbhatt

Rupali Deshpande
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
Hey,
thanks chandra.
It was really a silly mistake.

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

Rupali

 Yes, my master! Here is the tiny ad you asked for: free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter