<p>In addition, are there any performance advantages to static methods over instance methods?</p>

<p>I came across the following recently: <a href="http://www.cafeaulait.org/course/week4/22.html" rel="noreferrer">http://www.cafeaulait.org/course/week4/22.html</a> :</p>

<blockquote>
  <p>When should a method be static?</p>
  
  <ol>
  <li>Neither reads from nor writes to instance fields</li>
  <li>Independent of the state of the object</li>
  <li>Mathematical methods that accept arguments, apply an algorithm to those
  arguments, and return a value</li>
  <li>Factory methods that serve in lieu of constructors</li>
  </ol>
</blockquote>

<p>I would be very interested in the feedback of the Stack Overflow community on this.</p>

[{'answer_id': 48756, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 0, 'selected': False, 'text': '<p>Here is a related discussion as to <a href="https://stackoverflow.com/questions/23228/why-is-stringformat-static"><strong>why String.Format is static</strong></a> that will highlight some reasons.</p>\n'}, {'answer_id': 48759, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 6, 'selected': True, 'text': "<p>Make methods static when they are not part of the instance. Don't sweat the micro-optimisations.</p>\n\n<p>You might find you have lots of private methods that could be static but you always call from instance methods (or each other). In that case it doesn't really matter that much. However, if you want to actually be able to test your code, and perhaps use it from elsewhere, you might want to consider making those static methods in a different, non-instantiable class.</p>\n"}, {'answer_id': 48760, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 4, 'selected': False, 'text': '<p>Whether or not a method is static is more of a design consideration than one of efficiency. A static method belongs to a class, where a non-static method belongs to an object. If you had a Math class, you might have a few static methods to deal with addition and subtraction because these are concepts associated with Math. However, if you had a Car class, you might have a few non-static methods to change gears and steer, because those are associated with a specific car, and not the concept of cars in general.</p>\n'}, {'answer_id': 48781, 'author': 'Roddy', 'author_id': 1737, 'author_profile': 'https://Stackoverflow.com/users/1737', 'pm_score': 1, 'selected': False, 'text': "<p>Performance-wise, a C++ static method can be slightly faster than a non-virtual instance method, as there's no need for a 'this' pointer to get passed to the method. In turn, both will be faster than virtual methods as there's no VMT lookup needed.</p>\n\n<p>But, it's likely to be right down in the noise - particularly for languages which allow unnecessary parameter passing to be optimized out.</p>\n"}, {'answer_id': 48804, 'author': 'Ian', 'author_id': 4396, 'author_profile': 'https://Stackoverflow.com/users/4396', 'pm_score': 1, 'selected': False, 'text': "<p>@jagmal I think you've got some wires crossed somewhere  - all the examples you list are clearly not static methods.</p>\n\n<p><strong>Static methods should deal entirely with abstract properties and concepts of a class - they should in no way relate to instance specific attributes (and most compilers will yell if they do).</strong> </p>\n\n<p>For the car example, speed, kms driven are clearly attribute related. Gear shifting and speed calculation, when considered at the car level, are attribute dependent - but consider a carModel class that inherits from car: at this point theyy could become static methods, as the required attributes (such as wheel diameter) could be defined as constants at that level.</p>\n"}, {'answer_id': 48811, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 2, 'selected': False, 'text': '<p>Just remember that whenever you are writing a static method, you are writing an inflexible method that cannot have it\'s behavior modified very easily.</p>\n\n<p>You are writing procedural code, so if it makes sense to be procedural, then do it.  If not, it should probably be an instance method.</p>\n\n<p>This idea is taken from <a href="http://steve.yegge.googlepages.com/singleton-considered-stupid" rel="nofollow noreferrer">an article by Steve Yegge</a>, which I think is an interesting and useful read.</p>\n'}, {'answer_id': 48869, 'author': 'marcospereira', 'author_id': 4600, 'author_profile': 'https://Stackoverflow.com/users/4600', 'pm_score': 4, 'selected': False, 'text': '<p>Another problem with static methods is that it is quite painful to write unit tests for them - in Java, at least. You cannot mock a static method in any way. There is a <a href="http://googletesting.blogspot.com/2008/06/defeat-static-cling.html" rel="nofollow noreferrer">post on google testing blog about this issue</a>.</p>\n\n<p>My rule of thumb is to write static methods only when they have no external dependencies (like database access, read files, emails and so on) to keep them as simple as possible.</p>\n'}, {'answer_id': 31804541, 'author': 'SpaceTrucker', 'author_id': 1466267, 'author_profile': 'https://Stackoverflow.com/users/1466267', 'pm_score': 0, 'selected': False, 'text': '<p>Another thing to consider when making methods static is that anyone able to see the class is able to call a static method. Whereas when the mehtod is an instance method, only those who have access to an instance are able to call that method.</p>\n'}]
['https://Stackoverflow.com/questions/48755', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3475/']