开发者

Why we do not need to call Static Methods through the object?

开发者 https://www.devze.com 2023-03-08 19:29 出处:网络
public static void callit(ref int var) { var++; } public static void main(Object sender, EventArgs e) { int num=6;
public static void callit(ref int var)  
{  
   var++;  
}  
public static void main(Object sender, EventArgs e)  
{  
   int num=6;  
   callit(ref 开发者_运维知识库num);  
   Console.WriteLine(num);  
}

But if here method callit() would not be a static then I had to make object of class then call it.


That's correct. Non-static methods need to be called on an instance of an object. Even if the method doesn't actually use any other members of the object, the compiler still enforces the rule that instance methods require instances. Static methods, on the other hand, do not need to be called on an instance. That's what makes them static.


Exactly because that's the whole point of static methods.

Instance methods require to know which instance of the class you call the method on.

instance.Method();

and they can then reference instance variables in the class.

Static methods, on the other hand, don't require an instance, but can't access instance variables.

class.StaticMethod();

An example of this would be:

public class ExampleClass
{
    public int InstanceNumber { get; set; }

    public static void HelpMe()
    {
        Console.WriteLine("I'm helping you.");
        // can't access InstanceNumber, which is an instance member, from a static method.
    }

    public int Work()
    {
        return InstanceNumber * 10;
    }
}

You could create an instance of this class to call the Work() method on that particular instance

var example = new ExampleClass();
example.InstanceNumber = 100;
example.Work();

The static keyword though, means that you don't need an instance reference to call the HelpMe() method, since it's bound to the class, and not to a particular instance of the class

ExampleClass.HelpMe();


I think MSDN explains it very well

Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.

You can find more details about this topic here


This is simply a matter of C# syntax, if I understand your question correctly. There is no ambiguity in using callit(ref num); in your example. It is known exactly what method to call, since it is a static method and there is no object attached. On the other hand, if callit was not static, the compiler would not know the object on which to call the method, since you are calling it from a static method (which has no object). So, you would need to create a new object and call the method on that object.

Of course, if neither method was static, the method call would operate on itself, and again, the object would be known so there is no problem.


Because static methods are associated with the class itself, not with a specific object instance.


Static functions work on classes. Member functions work on instances.

And yes, you can only call a member function of an object (instance). No instance, no member.


Static methods are called on the type (or class), whereas non-static methods are called on the instance of a type i.e. object of a class.

You cannot call non-static methods or non-static variables in a static method, as there can be multiple objects (i.e. instances of the same type).


Static functions access class variables that are just as accessible by any other static functions AND instance functions. Meaning if you have a class variable called static int count, in static method static increaseCount() { count++; } increases the variable count by 1, and in static method static decreaseCount() { count--; } decreases the variable count by 1.

Therefore, both a static function and an instance function may access a static variable, but a static function MAY NOT access an instance variable.

Static functions are also called class functions. Non static functions are also called instance functions.


Static method are independent of instance, suppose there is man class in that there is eat method which is non static and there is sleep method which is static then when you create different instance of man lets say man m1, m2. m1 and m2 share same sleep methods but different eat method. In java all static variables are stored in heap memory which is shared all object instances suppose at runtime if static variable changes then it will share on all instances of the object in our case.m1 and m2. But if you change non-static varible it will for only that object instance

m1.anStaticvariable = 1; // changes value of m2.anStaticvariable also But m1.nonStaticvariable = 1; //wont change m2.nonStaticvariable

Hope this helps you

0

精彩评论

暂无评论...
验证码 换一张
取 消