Can non-static methods modify static variables

2019-01-07 09:30发布

I am wondering how a non static method can modify a static instance 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.

9条回答
成全新的幸福
2楼-- · 2019-01-07 09:54

Non static methods can access static variables. Static methods can access only static variables or methods directly without creating object.ex:public static void main(String arg[])

查看更多
ら.Afraid
3楼-- · 2019-01-07 09:55

Static members are not instance members , these are shared by class , so basically any instance method can access these static members .

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-07 09:55

Non-Static Methods can access both Static Variables & Static Methods as they Members of Class

Demo Code

public class Static_Class {
    protected static String str;
    private static int runningLoop;

    static{
        str = "Static Block";
    }

    /**
     * Non-Static Method Accessing Static Member  
     */
    public void modifyStaticMember(){
        str = "Non-Static Method";      
    }

    /**
     * Non-Static Method invoking Static Method
     */
    public void invokeStaticMethod(){
        String[] args = {};
        if(runningLoop == 0){
            runningLoop++;
            main(args); 
        }
        //Exiting as it will lead to java.lang.StackOverflowError
        System.exit(0);
    }

    public static void main(String[] args) {
        Static_Class instance = new Static_Class();
        System.out.println(str);
        instance.modifyStaticMember();

        // Changed Value persists 
        System.out.println(str);

        //Invoking Static Method
        instance.invokeStaticMethod();

    }
}
查看更多
迷人小祖宗
5楼-- · 2019-01-07 10:07

Yes, a static method can access a non-static variable. This is done by creating an object to the class and accessing the variable through the object. In the below example main is a static method which accesses variable a which is a non-static variable.

demo code:

public class Sample {

   private int a;

   void method()
   {
       System.out.println("i am a private method");
   }

   public static void main(String[] args)
   { 
       Sample sample=new Sample();
       sample.a=10;
       System.out.println(sample.a);
   }
}   
查看更多
太酷不给撩
6楼-- · 2019-01-07 10:08

I have found this from The Java Tutorials

  • Instance methods can access instance variables and instance methods directly.
  • Instance methods can access class variables and class methods directly.
  • Class methods can access class variables and class methods directly.
  • Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.

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

查看更多
冷血范
7楼-- · 2019-01-07 10:08

Look at it this way. A static variable can be accessed in many ways. One of the most common is to precede the var name with the class name, since static vars are per class. Since you refer to this variable in the same class, you are exempt from having to precede it with the class name. It does not matter where you call the static variable. Also this is a private static var not accessible by any other class.

查看更多
登录 后发表回答