• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing Objects by value - A 'simple' example.

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic