• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Objects into Methods()

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm slightly confused by the code below:



This gives the output:

INSIDE obj.n=3
OUTSIDE obj.n=50

So it seems when we pass an object into a method, we can modify its data, but we can't make it reference another object.

Can anyone explain since the explanation I read wasn't very clear.

Thanks
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All parameter passing in Java is by value.

When the formal parameter in the method is an object reference, a copy of the actual parameter reference is sent to the method.

As long as that copy still refers to the original object, you can change it, provided the object is mutable.

However, if you point the formal parameter to another object, it won't affect the original.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it may be easier if you change the code like this

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".
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent!

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.

I'll try it out.

Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try it out
That is often the best way to learn stuff. glad we could help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic