Why can't you call a non-static method from a static method?

后端 未结 9 395
北海茫月
北海茫月 2020-12-03 19:32

I have a static method [Method1] in my class, which calls another method [Method2] in the same class and is not a static method. But this is a no-no. I get this error:

相关标签:
9条回答
  • 2020-12-03 19:45

    Within a static method, you do not have an instance of the class. So it will be impossible to call an instance method on an instance when no instance exists.

    0 讨论(0)
  • 2020-12-03 19:46

    A non-static method requires an instance of the class. Unless you have passed in an instance, or created an instance in your method, you cannot call a non-static method, as you have no idea what instance of the class that method should operate on.

    0 讨论(0)
  • 2020-12-03 19:50

    Simple rule of thumb is:

    A static method/class/ctor can never ever [use/call/invoke/what the heck you name the verb] call a non-static field, property, or method. But, calling by means of instantiation.

    0 讨论(0)
  • 2020-12-03 19:58

    Static members can access only the Class’s other static members directly and a static method cannot access non-static members of the same class directly.

    A static method or property can call only other static methods or properties of the same class directly (i.e., using the method name by itself) and can manipulate only static variables in the same class directly.

    To access a class’s non-static members, a static method or property must use a reference to an object of that class. Recall that static methods relate to a class as a whole, whereas non-static methods are associated with a specific object (instance) of the class and may manipulate the instance variables of that object (as well as the class’s static members).

    Many objects of a class, each with its own copies of the instance variables, may exist at the same time. Suppose a static method were to invoke a non-static method directly. How would the method know which object’s instance variables to manipulate? What would happen if no objects of the class existed at the time the non-static method was invoked? Source

    0 讨论(0)
  • 2020-12-03 20:00

    In order to call non static method (i.e. instance method) you must have a instance of object of the method before you can call the said method.

    What you are actually trying to do is this. Note the this object in Method1. You do not have this available in static methods.

    static void Method1() {
       this.Method2()
    }
    
    void Method2() { }
    
    0 讨论(0)
  • 2020-12-03 20:06

    First you need to create instance of the class in order to call non-static method or functions.

    public class ClassHelper()
    {
       public static ClassHelper GetInstance()
        {
            ClassHelper instance = null;
    
            if (System.Web.HttpContext.Current == null)
            {
                instance = new ClassHelper();
                return instance;
            }
    
            if (System.Web.HttpContext.Current.Application["CLASSHELPER_INSTANCE"] == null)
            {
                instance = new ClassHelper();
                System.Web.HttpContext.Current.Application["CLASSHELPER_INSTANCE"] = instance;
            }
    
            instance = (ClassHelper)System.Web.HttpContext.Current.Application["CLASSHELPER_INSTANCE"];
    
            return instance;
        }
    
    
       public string Test()
       {
           return "test123";
       }
    
    }
    

    Then you can call the function as follows:

    textBox1.Text = ClassHelper.GetInstance().Test();
    
    0 讨论(0)
提交回复
热议问题