How to make a variable increment on every running of a method?
java, variables
Solution
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()`.
Problem
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); } } ```