Why can't you call a non-static method from a static method?
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:
An object reference is required for the non-static field, method, or property "ClassName.MethodName()"
Can some开发者_JAVA技巧one please briefly describe why? including other things that might be related to this.
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.
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.
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() { }
You need an instance of the class class to call the non-static method. You could create an instance of ClassName and call Method2 like so:
public class ClassName
{
public static void Method1()
{
ClassName c = new ClassName();
c.Method2();
}
public void Method2()
{
//dostuff
}
}
The static keyword basically marks a method as being call-able by referencing only its type [ClassName]. All non-static methods have to be referenced via an instance of the object.
Because a "static" method is what's known as a "class method". That is, you dispatch on an object one of two ways in a class-based language like C#, by class or by instance. non-static members can be dispatched to by other non-static members, conversely, static members can be called only be other static members.
Keep in mind, you can get to one from the other, by the "new" mechanism, or visa versa.
The static method by definition is not provided access to a this
. Hence, it cannot call a member method.
If the member method you're trying to call from the static method doesn't need a this
, you can change it into a static method.
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();
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
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.
精彩评论