Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects passed by reference

 
Michael Labuschagne
Ranch Hand
Posts: 56
Java Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
Michael Labuschagne
Ranch Hand
Posts: 56
Java Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic