   Confused by statics
  I''m learning C#, but I''m puzzled by statics, as I can''t seem to understand the way they work.I''ll attempt to explain how I see it and hopefully somebody will tell me where I''m going wrong...A class can have a variable ''A'', which may or may not be a static.If the variable is not static, then there is a separate ''A'' for every instance of the class (so the values of ''A'' can be different for each instance).If the variable is static, then then is a single ''A'' which is referred to by every instance of the class (so the value of ''A'' is the same for all the instances of the class).A class can also have a method ''M'', which may or may not be static. Now, applying the same behaviour as for a static variable to a static method seems to make no sense to me whatsoever, i.e.If the method is not static then there is a separate ''M'' for each instance of the class. If the method is static then there is a single ''M'' which can be used by all instances of the class.Unless this is simply a way of saving memory (by having one instance of code instead of potentially hundreds or thousands of identical pieces of code), I can''t figure out what the point of a static method of is - which implies that my understanding of them is wrong.Anyone care to point out the error of my ways? 
  Im not experienced with C# but i can explain static methods as they apply to C++ which may/may not be the same in C# but hopefully give you an idea.the difference between static and non-static methods (in C++ anyway) is not the same as static variables, your explanation of static variables is correct but static class methods do not have a this pointer which non-static methods do have, in effect you are saying that this function is related to the class it is declared in but does not operate upon objects of that class. Because of this you cannot access internal class data from a static member function and do not need to have an object of the class containing the static method in order to call it.   [edited by - Spudder on February 14, 2004 5:09:22 PM];  static member functions can only access static member variables.  ;  STATIC FUNCTIONS in C#There is a single function which can be used by all instances of the class, this is the same as non static functions.Difference between static and normal functions in C# is that a static function can be called without makeing any instances of that class at all.It's like calling a normal non-member function in C. Say you had a class like:class Math{int static Addints(int a, int b) { return a+b;}}you could then call"int c = Math.Addints(1,2);"from main, without creating any instances.STATIC FUNCTIONS IN C++I don't remeber static functions in C++ but non static functions can access and change static varables in the class."return StaticVarable;"is a common non-static function I think.-Note: I bet that the code is non-compiler yummy, just to show idea.mabey this helped you some? I don't know it's soo damn early here to make much sence, next time I will post after some sleep.   [edited by - DarkSol on February 15, 2004 4:03:14 AM];  Darksol''s explanation for C# also applies pretty well to c++. A class'' static functions exist independently of any instance of that class, so they can be called without refering to a specific object. The syntax is different in c++ though. ;  Everyone has explained it exellently. I''d just like to point out to the OP that now you should understand why in C# you must have a public static void Main(..) function.As there is no instances of any objects created when your program loads up the CLR (???) must first call a static member function in order to get everything going.  ;  Thanks to everyone who replied. The lightbulb has gone on and it''s much clearer now! ;  To be certain I understand, I''ve thought of some analogies...Static variables :There is a Boeing747 class. A static variable of this class could be ''wingspan'', as every instance of the class would always have the same wingspan size, and so the one variable is sufficient for every instance. A non-static variable could be ''airspeed'', as every instance of the class could be moving at a different speed, and so a separate variable is necessary for each instance.Static methods :A non-static method could be ''Move'' for the Boeing747 class. There would need to be an instance of the class to apply the method to.A static method could be part of a ''math'' class. E.g. ''Add'' - it is a function that exists without an object to work on, simply processing the data passed into it. ;  Ya that sounds right, it does make static methods seem kinda usless to me. I'm sure there are some good uses, other then like my quick example of the math class. Can't think of any more right now thou.Mabey a static method that takes a refence to a linked list and then creates a instance and addes it to the list. Of couse this could be done with contructors and is proply a better and cleaner way.HELP: Someone tell me some good uses of static methods, other like the math.ADD(), seems to me most of those simple examples will be allready done in the supplied librarys.hmm to make a custom operator it has to be static right?Edit: (weee! 5am run on sentences)   [edited by - DarkSol on February 15, 2004 7:23:47 AM];  Static member variables/methods are used in some implementations of Singleton classes where you only want a single instance of the class to be created and use a static function to retrieve the static instance from inside the class. 