Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

Passing multiple int's

Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay it's time for me to go full noob. I have absolutely no idea how to pass an into between to methods. All i want to do is pass a from the passing class to flag in the main class. This is the code i got made

the outcome is
Passing Flag = 555
a = 0
Main Flag = 0

i want the outcome to be
Passing Flag = 555
a = 555
Main Flag = 555

if any one can shed some light i will be grateful.
Java Cowboy
Posts: 16084
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not going to get the result you want with how the code is set up now, because Java strictly uses pass-by-value for method arguments. In other words, when you call the method fred, you're not actually passing the variable 'a', you're only passing the value of the variable 'a'. The 'a' in line 3 of class passing is a different variable 'a' than the one defined in the main() method.

I see that you're already returning a value from the method fred, but in class main you're not doing anything with the return value. In line 11 you're calling passing.fred(a); but you're ignoring the return value.

Try changing line 11 of class main to this:

That way, you assign the return value of the method fred to the variable 'a'.

Relevant tutorial topics:
  • Defining Methods
  • Passing Information to a Method or a Constructor
  • Returning a Value from a Method

    Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
    Free, earth friendly heat - from the CodeRanch trailboss
      Bookmark Topic Watch Topic
    • New Topic