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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

help me in understanding this program

Ranch Hand
Posts: 69
• Number of slices to send:
Optional 'thank-you' note:
hai im unable to understand the working of the program especially z.x = 6;
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;
} }
ppls explain in detail thanks in advance

Ranch Hand
Posts: 35
• Number of slices to send:
Optional 'thank-you' note:
Here z.x = 6; means that, the value of x (int) reference through the object of reference z (Fizz) is equals to 6.

As x(int) reference is avaialable for different object such as x (Fizz), y(Fizz) and z(Fizz). Each object can have differenct value for x (int).

So in this case, x(int) through z(Fizz) object have value as 6.

Ranch Hand
Posts: 1183
• Number of slices to send:
Optional 'thank-you' note:

prints true true because FizzSwitch makes f1 and f3 point to the same object.

Regards,
Dan

Ranch Hand
Posts: 451
• Number of slices to send:
Optional 'thank-you' note:
The question may want to test you something about final .
f1 is a final. X refers to f1. z is final and initialized to refer to x. X is non-final, but it can still refer to final variable/instance. z is final and z cannot be reassigned to some other instance. But z.x can be reassigned. In this example, you may
see that when z is assigned to x, z.x is 5. When z.x = 6, this x value encapsulated by z can be reassigned to a different value.

Helen Ma
Ranch Hand
Posts: 451
• Number of slices to send:
Optional 'thank-you' note:
Correct me if I am wrong:

Step 1. f1 --> a Fizz object with initial x=5 where ---> means refer

Step 2 x ---> the same Fizz object

Step 3. z ----> the same Fizz object

Step 4 z.6 means update the same Fizz object's x from 5 to 6.

Step 5 : return z means returning that Fizz object

Step 6: assign f3 to the Fizz object returned from step 5

Therefore f3 --> that Fizz object created in step 1 and therefore the x values are the same.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?