Static Variables and Methods

AP Computer Science A· difficulty 3/5

public class K {
    private static int n = 0;
    public static void inc() { n++; }
    public static int getN() { return n; }
}
// ...
K.inc(); K.inc();
K k = new K();
k.inc();
System.out.println(K.getN());

What is printed?

  • A

    3

    check_circle
  • B

    2

  • C

    1

  • D

    0

Explanation

The static counter n is incremented three times in total regardless of how inc is called.

Want 10 more like this — adaptive to your weak spots?

Related questions