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

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

An object reference is required for a non-static field, method, or property "ClassName.MethodName ()"

Can someone please briefly describe why? including other things that may be related to this.

+5
source share
8 answers

The non-static method requires an instance of the class. If you did not pass in the instance or did not create an instance in your method, you cannot call a non-static method, since you have no idea which instance of the class should work.

+22
source

In the static method, you do not have an instance of the class. Therefore, it is not possible to call the instance method on the instance if the instance does not exist.

+2
source

To call a non-stationary method (i.e. an instance method), you must have an instance of the method object before you can call the specified method.

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

 static void Method1() { this.Method2() } void Method2() { } 
+1
source

You need an instance of a class class to call a non-static method. You can create an instance of ClassName and call Method2 as follows:

 public class ClassName { public static void Method1() { ClassName c = new ClassName(); c.Method2(); } public void Method2() { //dostuff } } 

The static keyword basically marks the method as called, referring only to its type [ClassName]. All non-static methods must be referenced through an instance of the object.

+1
source

Because the "static" method is known as the "class method". That is, you send the object in one of two ways in the class, for example C #, by class or by instance. non-static members can be sent by other non-static members; conversely, static members can only be called by other static members.

Keep in mind, you can go to one of the other, to the "new" mechanism or vice versa.

0
source

The static method is by definition not given access to this . Therefore, it cannot call a member method.

If the member method that you are trying to call from a static method does not need this , you can change it to a static method.

0
source

First you need to instantiate a class to call a 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
source

Static members can directly access other static members of a class, and a static method cannot directly access non-stationary members of the same class.

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

To access non-stationary class members, a static method or property must use a reference to an object of this class. Recall that static methods belong to the class as a whole, while non-static methods are associated with a specific object (instance) of a class and can manipulate instance variables of this object (as well as static members of the class).

Many class objects, each with their own instances of instance variables, can exist simultaneously. Suppose a static method was to call a non-static method directly. How does the method know which instance variables of the objects are processed? What happens if class objects did not exist at the time the non-static method was called? A source

0
source

Source: https://habr.com/ru/post/1312483/


All Articles