Java - Calling void methods and storing their value
<p>This is a  question from a past exam paper. The question asks to declare a variable <code>c</code> of type <code>Counter</code> in a new class. I should then intialise it to a new instance of <code>Counter</code> with <code>n</code> taking a value of 100. I must then increment its <code>n</code> field twice. Then print the value of the <code>n</code> field to <code>System.out</code>.</p>

<p>I have done both classes, but i am not sure about the <code>Counter2</code> class. I think i may be declaring the <code>Counter</code> variable wrong.</p>

<pre><code>public class Counter {
    private int n;
    public int x;

    public Counter(int theN){
        this.n=theN;
    }

    public int getN(){
        return n;
    }

    public void inc(){
        x = 1+ getN();
    }
}

public class Counter2{
    Counter c;

    public int incrementTwice(){
        int i = 0;

        while(i&lt;2){
            c.inc();
            i++;
        }
        return c.x;
    }

    public static void main (String[] args){
        c = new Counter(100);
        int finalResult = c.getN();
        System.out.println(finalResult);
    }
}
</code></pre>
