This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Passing Objects into Methods() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Objects into Methods()" Watch "Passing Objects into Methods()" New topic
Author

Passing Objects into Methods()

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Oct 02, 2003
Posts: 11255
    
  16


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".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
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

Joined: Oct 02, 2003
Posts: 11255
    
  16

I'll try it out
That is often the best way to learn stuff. glad we could help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Passing Objects into Methods()