Regards, Manish
SCJP 1.4
Regards, Manish
SCJP 1.4
Regards, Manish
SCJP 1.4
Originally posted by Manish Sachdev:
I feel that variable sName is being shadowed in method piggy. So due to variable shadowing the orignal sName variable which belongs to the class does not change.
Originally posted by Manish Sachdev:
But when objects are passed to the method, a copy of its reference value is passed to the method. So will piggy method change the class level sName variable ??? as we passes the reference value of variable sName to the method piggy.
Originally posted by Alton Hernandez:
But if you are asking about the parameter itself, then the answer is it does not affect the caller. Parameters are passed-by-value, meaning, a copy of the value is made so the original will not be affected.
But when it comes to passing object reference, this may looks like passing by reference when in fact it is still pass-by-value. What is happening is that a copy of the address of the object is being made so the caller and callee will both have a copy of the address of the object.
Regards, Manish
SCJP 1.4
Mika Leino<br />SCJP, SCWCD
Regards, Manish
SCJP 1.4
Originally posted by Manish Sachdev:
As far as my knowledge, i know that when primitives are passed to the method, a copy of the value of that primitive is passed to the Formal parameter of the method. But when objects are passed, as u said that the reference value or the address of that object is passed to that method.
Originally posted by Manish Sachdev:
I thought that the sName (class level variable) is not getting changed due to variable shadowing, and it seems to be true.
Originally posted by Manish Sachdev:
BUT, my knowledge also tells me that in case of object, a copy of the reference value is passed, so how can the sName variable be shadowed, as we are passing the reference value, the reference value will remain the same as of class level and it should refer the same object to what class level variable refers and hence should not be shadowed.
Regards, Manish
SCJP 1.4