Forums Register Login

which values will be printed?

+Pie Number of slices to send: Send
[/code]
+Pie Number of slices to send: Send
did you run ?

P.S: Good Example to pass by value of reference and primitive

+Pie Number of slices to send: Send
Ans will be 4) 10020
+Pie Number of slices to send: Send
answer is 4. 10020
+Pie Number of slices to send: Send
good question answer will 10020
+Pie Number of slices to send: Send
We know that the answer will be 10020, but can someone explain, why at the and we get 20? Thanks
+Pie Number of slices to send: Send
code at line 20 changes the value of int i of the object instantiated at line 12
+Pie Number of slices to send: Send
 

Kamil Wojcik wrote:We know that the answer will be 10020, but can someone explain, why at the and we get 20? Thanks



At line 20, the another() method changes v.i to 20. Remember, when you pass an Object reference to a method, you pass a copy of the reference, not a separate copy of the object. So now, v.i points to the same v that was declared in line 12. So when the another() method completes and we resume at line 15, the value of v.i has been changed to 20. Does that make sense?

This is an excellent question.

Ben
+Pie Number of slices to send: Send
Hey when you pass a referance variable to a method you are not only passing the address of that referance variable but that is copy of that referance variable address.
This means that you are passing copy of referance variable to ValHold v.
Then you are changing that value to vh(another object) The previous referace object is still same uneffected.

Remember you can always change the value of the passing object but you can't change the passing object to refer to other object.

+Pie Number of slices to send: Send
Hey when you pass a referance variable to a method you are not only passing the address of that referance variable but that is copy of that referance variable address.
This means that you are passing copy of referance variable to ValHold v.
Then you are changing that value to vh(another object) The previous referace object is still same uneffected.

Remember you can always change the value of the passing object but you can't change the passing object to refer to other object.

Answer is abc.
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1130 times.
Similar Threads
call by reference
weird call by ref question
instance variable
Question on References
Can someone explain!!!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 18:52:08.