aspose file tools
The moose likes Beginning Java and the fly likes Passing Objects into Methods() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 712
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: 2341
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: 9950
    
    6


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


Never ascribe to malice that which can be adequately explained by stupidity.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 712
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: 9950
    
    6

I'll try it out
That is often the best way to learn stuff. glad we could help.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Passing Objects into Methods()
 
Similar Threads
system.out.println()
confusion in if condition
A question on object cloning.
Possible Memory Leak ?
Anonymous inner class