Originally posted by kaushik vira: Ans is pretty much clear..
you are using final key word at class level.. so now no class can inherit Fizze class. nothing else
in FizzSwitch function you doing nothing.. it`s not at tall confusing ..
I am not sure if "final" has to do anything with the output of this program. You can as well remove final to get the same output. I think the emphasis of the question is to demonstrate that changes made in an object inside a method are visible outside too. Another point demonstrated here is that though the references may be final, the objects being pointed by them are still modifiable.
Place your code in code tags so that its easily readable. Satya is correct. When main is invoked it creats two Fizz objects and assigns them to reference variables f1 and f2. The instance varaible x within these two objects have 5 as the value of x. Now when FizzSwitch() is invoked it assigns the reference varaibles f1 and f2 to x and y. So now the two Fizz objects that were created in main() will have four reference variables pointing to them f1 and x point to first Fizz object and f2 and y point to second fizz object. The FizzSwitch() creates a new reference variable z and points to x and therefore z points to the Fizz object that was pointed by f1. Now f1, z, and x all point to the first Fizz object [this object has x value as 5]. So when z.x=6 statment executes it modifes the x in the first Fizz object [pointed by f1, x and z]. FizzSwithch() returns z and this is assigned to f3 and now f3, f1 will point to first Fizz object [F.x is 6]. Therefore in main f1==f3 will return true and f1.x==f3.x will return true because f1 and f3 both point to same fizz oject. This code demonstrates that changes made to the object is reflected across methods. The result will be same with or without declaring z in FizzSwitch() final, as final will not allow z to be assigned to another fizz object or null. Thanks Deepak
SCJP, SCWCD, SCBCD
Joined: Aug 05, 2006
These concepts become very simple when you visualize them. That is try to draw pictures on paper and modify after every statement execution. This will help you understand questions easily. Remeber A picture is worth 1024 Words Thanks Deepak