• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Objects by value - A 'simple' example.

 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code below is from a Java book, from a chapter on Methods and Classes. It apparently illustrates the concept of passing objects by value. The thing is, I can sort of follow what's going on, but:-
<B> ob.meth(a, b); </B>
- passes the values 10 and 20 to the 'meth' method. OK, but what happens to the values inside of this method? Surely their values are changed?! The program, when run, states that their values have NOT changed! What's going on folks, i'm stuck [again].
Any help would be greatly appreciated. Cheers in advance.
 
Dan Walin
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that a COPY of a and b are sent to meth. The a and b that are in main() remain unchanged. It's like if I faxed you a document and you took that document and wrote all over it. That COPY has been changed but my original document is the same as it was. Also, meth() looks like it's not returning anything. If you had something like a = meth(a), then you would be sending a copy of a to meth and you could return a result in with which you could then modify a. I'm a Java beginner so don't give this post more weight than it deserves but if it helps that's good too.
 
Tony Matthes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,
From what i can see your problem is that your 'meth' method is void,
this means it does not return a value to the method from where it was called.
what you need is a method that returns int, e.g.

if you wanted to return a String your method would be:

So, since you are returning values, you can output these in 2 ways, you can create two ints to store your values in:
eg

and output value_to... to the user
or you can simply go:

I hope this is of some help to you
Tony
[ November 16, 2003: Message edited by: Tony Matthes ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan --
Your answer is good. You guys both might want to check out the JavaRanch campfire story on this topic, here. You should read the Cup Size story first.
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool.
I've sussed it, and I wouldn't have had it not been for the input of all those who replied.
Cheers folks, thanks for your help.
No doubt I'll have loads more problems, as i tackle Java. :roll:
Just hope I can post these problems as I encounter them!
[ November 16, 2003: Message edited by: Steve Jensen ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan,
I agree with Ernest: I particularly like the fax analogy.
That's it. Just a comment. Nothing more to add.
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that if an array is passed to a method, the original value WILL be changed. However, if a single array value (array[3]) is passed, that value is not changed. Is that right, everyone?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic