javamethodsstaticinstance-variablesaccess-modifiers

Can non-static methods modify static variables


I am wondering how a non static method can modify a static variable. I know that static methods can only access other static methods and static variables. However, is the other side true? Can non-static methods access only non-static variables? For example:

public class SampleClass {
  private static int currentCount = 0;

  public SampleClass() {
    currentCount++;
  }

  public void increaseCount() {
    currentCount++;
  }
}

This code compiles and I would like to know why in terms of static access privledges.


Solution

  • I have found this from The Java Tutorials

    So the answer is yes, non-static methods CAN modify static variables