• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

explain this code

 
anamika henry
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rupali Deshpande
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is that error Rupali?
Copy paste here!!




Regards,
cmbhatt
 
Rupali Deshpande
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

The error is
"FizzSwitch cannot be resolved to a type" at line 6
 
lalitha kalanadhbhatla
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
thanks chandra.
It was really a silly mistake.



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

Rupali
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic