• Post Reply Bookmark Topic Watch Topic
  • New Topic

altering a variable with mutiple methods  RSS feed

 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i have a variable x. i want this varable tout passed to a method have a vaule added to it then be passe to the next an have more added to it



basicly take value as a zero pas to a methed an have it keep th vaule it was turned to within the method then passed to the next to add more
 
Yosuf Ibrahim
Ranch Hand
Posts: 128
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The reason you are getting 0 as an answer is that you are giving the methods the initial 0 but you are not returning the added value anywhere, so whatever the methods return is collected later by the garbage collector.

Another way you can do it which is non-conventional I am sure is as follows but will get the same result.

 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so if i put in value i need to return value?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Stallard wrote:so if i put in value i need to return value?

In your case, yes, because your parameter "value" is a local copy. If you modify a local variable that is either a built in type (e.g. int) or an immutable object reference, then any operations have no effect on the values in the original caller. If you want the caller's value to change then you have to return the new value from the method and assign it to the caller's variable.

For methods in general: just because you pass in a parameter doesn't mean you need to return something. Conversely, just because you return something doesn't mean you need to pass a parameter in.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so this


should be

 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry the last snipet should be

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel wrote:

or...
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so would it affect it if i mad a print line in there. like
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Stallard wrote:so would it affect it if i mad a print line in there. like

You  could do that. It would print the new value. It does not guarantee that the caller assigns the returned value to its copy of its "value" variable.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so line 3 affects my return value
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Stallard wrote:so line 3 affects my return value

Your print statement DOES NOT AFFECT the contents of "value".
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Stallard wrote:so would it affect it if i mad a print line in there. like

Note that line two needs a semicolon at the end, line four needs to be and you are missing a close brace (}).  Please be precise when writing code.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, I know of those errors. I'm just trying to find out more about passing an argument and returning a result and adding it to a variable
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just want to say thanks for all the help guys on this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!