• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passed by reference or by value?  RSS feed

 
Ryan Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having some trouble figuring out whether the following GridBagConstraints c is being passed by reference or value to a method: It's really important that it be passed by reference, which is how I understood Java works...


In other words, when the method initGridBagConstraints is called, will it re-initialize origC as well as passedC?

[ July 18, 2004: Message edited by: Ryan Smith ]
[ July 18, 2004: Message edited by: Ryan Smith ]
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't going to do what you're trying. There are all kinds of discussions about Java and pass by value and pass by reference. The easy thing to remember is that you can modify an object by calling methods on it. You can never reassign an object that was passed into a method. For example -



The above will infact change the name to Sam. If instead you did this -


You won't get the name change, as you can't modify where myObj points to from within a method. Technically, java is always pass by value.

Here's some more insight -

http://www.javaranch.com/campfire/StoryPassBy.jsp

HTH,
Aaron R>

PS - If you aren't going to do anything else in your init method, then you should just do a new wherever you would have called that method.
 
Ryan Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woohoo!!! I'm done!!! YAY!!! Thanks! And thank you very much for the informative value vs. reference explanation - the book I'm using doesn't explain beyond a simple definition.
[ July 18, 2004: Message edited by: Ryan Smith ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!