This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Objects passed by reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects passed by reference" Watch "Objects passed by reference" New topic
Author

Objects passed by reference

Michael Labuschagne
Ranch Hand

Joined: May 08, 2007
Posts: 56

In reference to the question and answer below, got asked this question in the rules roundup. I don't quite agree though.

Why would java pass a copy of the reference to the original object? It doesn't makes sense, I thought that a copy of the reference gets passed to the method. Or am I not understanding the question?

QUESTION:
=========

(#8) When you pass a reference to an array as an
argument to a method call, you pass a new reference
to the original array.

Answer: true
An array is an object, and Java always passes a copy of
the reference to the original object. Both references now
point to the same array object.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
The answer has the potential to be misunderstood, due to its phrasing, and I think you have done so.

Using square brackets to split up the clauses (if that's the right term), I think the writer meant: -

"An array is an object, and Java always passes a copy of
[the reference to the original object]."

... whereas it seems you think they meant: -

"An array is an object, and Java always passes a copy of
[the reference] to the original object."


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Peter Chase:
..."An array is an object, and Java always passes a copy of
[the reference to the original object]."...

Maybe it should be rephrased to something like: "A reference to the original object is passed to the method."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Michael Labuschagne
Ranch Hand

Joined: May 08, 2007
Posts: 56

Thanks Peter, that does make a lot more sense. But now, back to question. If you read the part I've bracketed, below, it seems to imply that the reference itself is changing. Why "new reference", as if a new reference is being created as a result of passing the object to the method?

Thanks you're a great help.

QUESTION:
=========

(#8) When you pass a reference to an array as an
argument to a method call, (you pass a new reference
to the original array).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Michael Labuschagne:
Why "new reference", as if a new reference is being created as a result of passing the object to the method?
Because a new reference IS being created. That new reference is assigned to the parameter.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Objects passed by reference
 
Similar Threads
If objects can't be passed....
how are the objects passed by value or by reference
passing a reference as an argument to a method
Rule Round-up #8
Vector passed by value or by referance???