 <p>If you declare the variable outside of a method, the state is remembered.</p> <p>A solution could be:</p> <pre><code>public class Test { int count = 0; public static void main(String[] args) { Test test1 = new Test(); test1.doMethod(); test1.doMethod(); test1.doMethod(); } public void doMethod () { count++; System.out.println(count); } } </code></pre> <p>This way <code>count</code> is created the moment you call <code>new Test()</code> and will be remembered until the Object is destroyed. Variables have something called a 'scope'. They can only be accessed in their scope and will only exist in that scope. Because you created <code>count</code> inside your <code>void doMethod()</code> method, it did not exist anywhere else. An easy way to look at the scope is by watching the brackets <code>{ }</code> your variables are only stored inside those brackets. In my solution the brackets for <code>count</code> are the brackets for the entire <code>Test</code> class. Thus the variable is stored until the Object is destroyed.</p> <p>More on scope: <a href="http://en.wikipedia.org/wiki/Scope_(computer_science)" rel="nofollow">http://en.wikipedia.org/wiki/Scope_(computer_science)</a></p> <p>I just noticed you mentioned you want the <code>count</code> value to remain after you run the program. You can do this by saving it in a database or file. However, you might have meant that you want the <code>count</code> value to remain after you run the <code>void doMethod()</code> method. I have edited my solution to execute the <code>void doMethod()</code> method three times so you see the value actually remains after running the method.</p>