aspose file tools*
The moose likes Beginning Java and the fly likes Passing Objects by value - A 'simple' example. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Objects by value - A Watch "Passing Objects by value - A New topic
Author

Passing Objects by value - A 'simple' example.

Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126
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.


John Bonham was stronger, but Keith Moon was faster.
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
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

Joined: Nov 16, 2003
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126
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

Joined: Aug 20, 2001
Posts: 1821

Dan,
I agree with Ernest: I particularly like the fax analogy.
That's it. Just a comment. Nothing more to add.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
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?


David Crossett
-nothing important to say, but learnin' plenty-
 
Don't get me started about those stupid light bulbs.
 
subject: Passing Objects by value - A 'simple' example.