• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing multiple int's  RSS feed

 
jack pashley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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

  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!