you create outerObjRef, which points to a specific place in memory. when you pass it to the method, you pass a copy of that address. so now, inside the method, innerObjRef points to the same place.
you then say with "innerObjRef.n=50;" go to the memory address, and change the n part to be 50. it's like giving me your home address, and a painter your home address. we both know how to find your house. if you tell the painter to paint your house blue, when I go there, your house will be blue.
at any rate, you then create a new object inside the method. when you say "innerObjRef=secondObj;", you are saying "erase that address of the card, and put this NEW address on the card." You have just given the painter your summer home address.
when you then say "print n" inside the method() method, your asking the painter to go to the house at the address on the card and tell you what color it is. He goes to your summer house, and tells you "white".
you leave the method, and come to the outerObjRef, and ask it. You are now asking me to go the house listed on MY card, so I tell you "blue".
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Apr 11, 2005
I get it now, thats kind of the same explanation I was told ages ago on my java course, but I wasn't paying attention, but I remember him saying you pass a copy of the address into the method.
I guess we can do the same thing with an array...
Then outside the method, array will just have its element-3 changed.