Can non-static methods modify static variables

前端 未结 9 2009
猫巷女王i
猫巷女王i 2020-11-29 01:53

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 ot

相关标签:
9条回答
  • 2020-11-29 02:00

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

    0 讨论(0)
  • 2020-11-29 02:05

    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);
       }
    }   
    
    0 讨论(0)
  • 2020-11-29 02:06

    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

    0 讨论(0)
  • 2020-11-29 02:08

    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();
    
        }
    }
    
    0 讨论(0)
  • 2020-11-29 02: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.

    0 讨论(0)
  • 2020-11-29 02:14

    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[])

    0 讨论(0)
提交回复
热议问题