The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Passing objects to a method.

Posted by Dmitry on July 30, 2001 at 1:54 PM

Well your generalization that all objects in java are passed by reference is not entirely correct. Java does not pass by reference, it will always pass a copy of the object (a pointer). So your swap method will not work. What will work is if you change the values of value variable then you perfomed an action on the instance of the object passed in and your changes will be visible when you come out of the swap method. The way I keep it organized in my head is - if you pass an object in and then try to change the reference stored in the variable to a different object nothing will happen when you come out of the method, if you operate on the referance (by calling objects methods of chaning of the properties by setting instance variables) changes will be visible when you come out. I hope this is clear enough.



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us