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:
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
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
Joined: Feb 01, 2007
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.
Joined: Oct 17, 2006
See if you can follow this.
[ February 03, 2007: Message edited by: David McCombs ]
Joined: Feb 01, 2007
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Passing value from one method to the next