Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Having trouble with passing parameters to methods

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I can't get b to print out to be 32 and I keep getting 16.



I also tried this but it also didn't work.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/how-to/java/CallByReferenceVsCallByValue
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All variables in java are 'pass by value'. So when you pass b into your method, you pass in the value 16, which then gets assigned to your 'a'. So 'b' is pointing to one spot in memory that holds the value 16, and a is pointing to a DIFFERENT spot that also holds 16.

you do a *= 2;, which does double what a is pointing to, but the value b points to is untouched.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela,

Please go through the link that Henry has provided, that should solve your problem.

For the specific example that you have posted, instead of passing objl.b, try and see what happens when you pass objl itself.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angela Li wrote:I also tried this but it also didn't work...


Try this instead:Lessons:
1. Methods, in order to be useful, generally return something, so don't use void unless you know it's correct (eg, for a 'setter' method, or for main() itself).
2. Resist the urge to make everything static.
3. Make instance fields private unless you have a good reason not to.
4. It may be a bit early yet, but try to get into the habit of writing classes, not main() methods.
5. Read what the others have already told you about 'pass by value'.

HIH

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic