aspose file tools*
The moose likes Beginning Java and the fly likes Passing value from one method to the next Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing value from one method to the next" Watch "Passing value from one method to the next" New topic
Author

Passing value from one method to the next

James Farber
Greenhorn

Joined: Feb 01, 2007
Posts: 6
Just as the subject implies, I'm having difficulty trying to pass a value from one method to the next in the same class. I know it has to do with object creation, but I can't quite get it working. Some sample code of what I'm trying to do:

David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Make number a class level variable. In your code you never call justSomeMethod, so number never gets initialized or incremented.



"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

You have declared number inside the justSomeMethod(). That makes it a local variable. In other words, that variable only exists inside that method.

in your main, you create an Example d. You call d.differentMethod(). At this point, since you've never entered the justSomeMethod method, the variable you are trying to acces does not exist.

David shows you how to move your local variable to become a member variable. Now it will exist as soon as an instance of the class is created. When you then call your differentMethod, you can access the variable.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
James Farber
Greenhorn

Joined: Feb 01, 2007
Posts: 6
Thanks for the help guys!
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Wow, I would have gone in a totally different direction and assume that if differentMethod() needed a number, it should be passed one as a parameter, especially if no other behavior of Example class depends on the current state of the *number* variable.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
James Farber
Greenhorn

Joined: Feb 01, 2007
Posts: 6
That's what I was doing initially, trying to pass it as a parameter. I couldn't quite get that down. I know a bit of C++ and can pass values between functions that way (or at least I used to know how a few years back), but I just couldn't figure it out in Java. Honestly, parameter passing would be ideal for me in this situation, since I'm now having issues of passing all sorts of different values, including values to/from methods in separate classes.
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
See if you can follow this.

[ February 03, 2007: Message edited by: David McCombs ]
James Farber
Greenhorn

Joined: Feb 01, 2007
Posts: 6
I can follow it. Tricked me at first, but I got it now. How does an object get passed between parameters? Passing the object from one parameter to the next in the same class is the same as passing it between parameters in different classes, right? Sorry if I'm asking too many questions for this thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing value from one method to the next