Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

help me in understanding this program  RSS feed

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Firefox Browser Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

Regards,
Dan

 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!