7

The way I understand it, having a member variable declared as static in a no-static class causes that member variable to be unique no-matter the number of instances of that class.

Now, what happen to a static method declared in non-static class? And (Most importantly), what the point of declaring a static method in a non-static class?

Thanks for helping

Richard77
  • 20,343
  • 46
  • 150
  • 252

7 Answers7

21

If the method has something to do with the type but not the instance then it can be static.

DateTime.Parse and Int32.Parse are examples.

cjk
  • 45,739
  • 9
  • 81
  • 112
5

It's useful for creating factory methods which are not members of any object but which need access to the internals of an object in order to initialize it.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
5

You need static methods in non-static classes f.e. for the factory pattern (if the class is a factory for itself, as pointed out by Jaco Pretorius):

  MyClass newInstance = MyClass.Create();

Or if you want to create helper methods. F.e. you could write an FTP-Class which is fully working, with Connect(), SendCommand(), ReceiveAnswer(), Disconnect() etc., but you want to allow the user to easily upload one file, without the need to implement the whole procedure:

  MyFTPClass.UploadFile("username", "password", "pathToFile");
Bobby
  • 11,419
  • 5
  • 44
  • 69
  • 1
    I'm being pedantic - your first sentence is only correct if the class is a factory to itself. When I use the factory pattern I will usually create separate factory classes, since it makes testing a little easier. – Jaco Pretorius Jul 13 '10 at 14:06
4

Class method which works only with its parameters, doesn't call any instance methods and doesn't work with any instance members, may be declared as static. Actually, it should be declared as static, for better performance, since static method doesn't require "this" pointer.

Consider small function which belongs to class, makes some calculations with its parameters and returns calculated value. This function should be static.

Alex F
  • 42,307
  • 41
  • 144
  • 212
  • 1
    I'd rate static correctness checking higher than performance. By declaring a method as static you're letting the compiler know that it can't use any information specific to particular instances, so it will throw errors or warnings when the programmer tries to do that anyway, e.g. tries to call it as an instance method. – reinierpost Mar 24 '11 at 09:36
3

It would be impossible to implement the Singleton pattern without being able to declare a static method (and private member variable) on a non-static class.

See Singleton

Johann Strydom
  • 1,482
  • 14
  • 18
2

For example you have a class for example Bank_Account in which you want to calculate the number of object created for that class.
So, you have one static field say count. Then when you initialize any object of class Bank_Account then you need to increment the field which stores the number of objects created for this class, the method incrementing this static variable is static as it is same for all the objects created for this class.

Himadri
  • 2,922
  • 5
  • 32
  • 56
  • Why this answer is downvoted??? :-( It is also saying what `ck's` answer is saying but it is the example of such method!! – Himadri Jul 13 '10 at 11:42
  • 1
    I don't understand a downvote either: seems to me this is a vital point. If you have data that is about all instances or any instance, rather than about one particular instance, then it should be static. For example, just yesterday I created a class where I need to assign each instance a unique id. Making a static counter was an easy and logical way to do this. – Jay Jul 13 '10 at 13:46
1

As ck said, it can be methods that has something to do with the type. It is important to remember that this will be a utility function, that will not be able to access any member variables in the type/class, since it may be called directly without having any instance of the class. If you try to define a static method that accesses a member variable (or non-static method), you will actually get a compiler error.

awe
  • 21,938
  • 6
  • 78
  • 91