I am trying to get the int count to increment each time I run the program. ie: So if I ran the program 9 times, and doMethod was called 9 times, the value of count would be 9. But since I have to initialize count to = 0 count keeps resetting itself to 0 on every iteration of the method. Is there a way around this?
public class Test {
public static void main (String[] args) {
Test test1 = new Test();
test1.doMethod();
}
public void doMethod () {
int count = 0;
count++;
System.out.println(count);
}
}
Instead of making it as a local to method, make it as instance member.
int count = 0;
-----
public void doMethod() {
count++;
System.out.println(count);
}
So that it wont reset to 0
on each call of doMethod()
.
If you want to increment count each time you run the program,
counter
variable count into a file or a database table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With