well lets start from line 12. AS there is a new object of Foo is created the value of barNum for that object is '0' .
then we have which passes the bar object as a reference.
In line 5 it prints the value of 99 as the value of barNum is changed to 99.
In line 6 a new object of Bar is created and thus the reference to the previous bar is lost for that variable, which now points to a new object.
In line 8 420 is printed since that is the value for the new bar object.
Now when we return to line 14 the value for barNum is going to be 99 since that is the last time the value has been changed for that object.
The object passed as an argument is shadowed by the creation of a new object in the method.
It is just an example of shadowing an instance variable by declaring a local variable of the same name and the shadowed variable is an object reference. The local variable myBar in changeIt() method is affecting myBar instance variable that is referring to an object of type Bar. myBar parameter receives reference to same Bar object.
Yes, I see now, the Instance myBar object reference is shadowed by the myBar Bar reference of the argument to the changeIt() variable (local variable) So when we are acting upon the myInt variable we are actually acting upon the local variable, but it can seem as if we are working on the instance variable.
be a well encapsulated person, don't expose your privates, unless you public void getWife()!