7

I'm trying to understand static methods and I've reached a confusing point.

Focusing only on methods for this question, if I create an instance of my object (where the class itself is not static) then I typically only have access to the public, protected and or internal methods (depending on scope/encapsulation). In other word, I don't have access to private methods.

I've read that, although minimal, static methods are slightly more efficient than non-static methods.

So, when creating a private method with a return type of void, and excluding when you're creating a reference of an object from within itself, why would you ever not make it static? All the code I've ever seen doesn't do this, so I can only assume I've missed the point.

Dave
  • 8,163
  • 11
  • 67
  • 103
  • 3
    For the same reason why you should not make everything `static`, because you want to benefit from OOP. Therefore you need instances. Even private methods are normally related to an instance. – Tim Schmelter May 14 '13 at 13:43

7 Answers7

30

Static methods cannot access the non-static member data in the class.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
6

Static methods are typically supposed to be stateless and therefore have no access to the instance state. In contrast, instance methods are stateful and can therefore read and modify the instance's state.

Typical examples of stateless methods are:

  • Factory Methods
  • Binary Operators
  • ...

Of course, static methods aren't always stateless though, there are samples of static stateful methods. There is one single state for the class then:

  • Singleton
  • Instance Pools
  • ...

These implementations need slightly more care though, since the class's state is also shared by all threads within the process.

Matthias Meid
  • 12,455
  • 7
  • 45
  • 79
1

I've read that, although minimal, static methods are slightly more efficient than non-static methods.

That's not unconditionally true: only the methods that could otherwise be static but are not made static by omission would be more efficient. Otherwise, you would need to pass a reference to the object manually, leveling up the playing field. Moreover, CLR is optimized so much that the difference is hard to measure.

To answer your question, there is no reason to make a method non-static if it does not access instance state through properties or variables. However, all methods that access per-instance state should be non-static for readability, because there is no performance to gain from making them static and passing the instance manually.

To illustrate the point, you should do this

private void AddCount(int number) {
    current += number;
}

rather than this:

// Do not do this!
private static void AddCount(MyClass obj, int number) {
    obj.current += value;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

You can't access non-static members in the class if you do this. You could pass in any instance variables as parameters to a private static function, but especially when a function interacts with a lot of instance data, this can lead to some very bloated and hard to read code. If you are operating on instance members of a class, and it's not an operation that can be done without an instance of your class, you should not make it static.

As a basic general rule, I do not make variables or functions static unless it is critical to all instances of the class. There are of course exceptions to this, but if you simply make all of your private methods static for no reason, you are likely going to be working against the OOP paradigm.

codewario
  • 19,553
  • 20
  • 90
  • 159
0

Most of the methods you see in code will somehow use class variables/properties that are not static. These cannot be accessed from a static context. This means that within a static method, you can only access static members of this class and not the object specific ones.

Toon Casteele
  • 2,479
  • 15
  • 25
0

I think it is very useful to at least define methods as static to make it clear they don't need data from an instance. That said, I prefer nonmember functions. Check out Class design vs. IDE: Are nonmember nonfriend functions really worth it?.

Community
  • 1
  • 1
ctn
  • 2,887
  • 13
  • 23
0

Two reasons come to mind:

  1. you cannot access other non-static members of your class (mentioned above)
  2. you cannot override static method in subclasses

IMO, static methods should be more as an exception. If you want an "easy access" to a functionality in an application with dependency injection container, you are better of with using a singleton bean and injecting it, because you will still have an option to switch implementation easily, if you ever need to.

.NET has a specific use case for static methods - extension methods. So if you want your functionality available as an extension method, you have to use static.

j_maly
  • 1,091
  • 1
  • 13
  • 27