wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Attempting to reset a loop/ method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Attempting to reset a loop/ method " Watch "Attempting to reset a loop/ method " New topic

Attempting to reset a loop/ method

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84

I have the following code which outputs the total value of all inventory in my program. However the problem is, it does not get reset each time the method is called.

For example it will output:

$500 the first time, which is correct
$1000 not correct
$1500 the third time which shows it is always adding on to itself, and not doubling.

Is there anyway to ensure that this mehtod is reset so the next time it is called, it starts from 0
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
something else is going on besides your loop not being reset. The fact that you have declared i and set it to start with 0 in the for condition should cause it to start fresh each time the method is called. Also, once the loop is started there does not appear to be anything that changes the value of i value other then each iteration of the loop.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
The solution is to add sumWhole=0; as the first line of code.

Not sure why i dident try that first, instead of last...
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
ahhh, OK, that makes sense, I clearly missed the point of your original post.

Still, something doesn't feel right. the variable sumWhole is not declared within the method, so I guess it is an instance variable of the class this method is in.

Now, you are calling this method either from within this class, or from another class. Which is it?

So your method resets an instance variable each time it is called, then applies some math to it, then returns the instance variable, I guess there is a good reason for that, but it makes me think about taking a second look at how your variables are defined, maybe sumWhole should be a local variable?

Then again maybe it is ok, I guess it depends on the program.

Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Well, that always remains the problem with globals. Here as far as I can see, sumWhole is a class level variable and lives till the lifetime of that object.

As per your requirement, sumWhole need not be a class level variable. It should be a local variable in the method : getWhole()


Experience and talent are independent of age
wood burning stoves
subject: Attempting to reset a loop/ method
Similar Threads
array .length not working as planned
Adding a new array element is not saving to memory
Trying to add a count to my inventory through a constructor
Still working on this inventory project, issue with inventory array
Weird multiple output issue with if statements