There's an important difference between the two methods: using hello(String)
you're trying to change the reference to the String
, and with hello(CustomObject)
, given a reference, you're using the reference to change a member of the object.
hello(String)
takes a reference to a String
. In the function you're trying to change which object that reference points to, but you're only changing the pass-by-value copy of the reference. So your changes aren't reflected outside the method.
hello(CustomObject)
is given a copy of a reference to an object, which you can then use to change the actual object. Think of this as changing the contents of the object. So your changes are reflected in the caller.
Given a reference to an object, you can change the object using it's exposed methods/fields