Take the 2-minute tour ×

I have some methods which allow me to lookup some identifiers, so essentially the run an SQL query and pass back a value.

Currently they are not static. Would it make sense to make them static, from a performance point of view i.e. have less in memory and save the time that is required to instantiate the object? Or are these considerations not really important.

share|improve this question
Looking a way to optimize that SQL would be a much better spent time here, in my opinion. –  Goran Jovic Dec 25 '10 at 13:32
Perhaps you are looking for a way to cache the results so that you don't constantly hit the database? If so, look in to a singleton pattern or use a caching framework. –  Jonathan B Dec 25 '10 at 15:15
add comment

2 Answers

up vote 5 down vote accepted

A reason to use a static method is if you believe it improves clarity.

Being pedantic, using static could gain 8 bytes of memory (one reference) The value of this memory is about 10,000 th of one cent. Using a static call saves as much as 1 ns, an SQL query might take 10 ms, so the difference is 10,000,000 times or more.

In 99% of cases, using static will save you less than the cost over having to type the word (i.e. the value of your time to type the word is much greater than the memory/processing you will save) Use static if you believe it makes the program clearer. e.g. for writing utility methods.

share|improve this answer
Thanks, that's a very convincing answer. –  Ankur Dec 29 '10 at 14:49
Or you could use an editor that types the word for you, and never have to waste time typing it again. =) –  Bruce Connor Aug 7 '12 at 1:15
Working in C# you are bound to encounter the "Mark members as static" FXCop rule from Microsoft.Performance: msdn.microsoft.com/en-us/library/ms245046.aspx "After you mark the methods as static, the compiler will emit nonvirtual call sites to these members. Emitting nonvirtual call sites will prevent a check at runtime for each call that makes sure that the current object pointer is non-null. This can achieve a measurable performance gain for performance-sensitive code." Isn't this an issue in Java (and Android)? Thanks. –  Jamrelian May 7 at 16:22
@Jamrelian Not so much. The JIT can work out how many different classes are called from a specific invocation. If there is only one or two classes, it can use a special invoke for those classes and even inline them. (If you aren't amazed by that, consider those classes can be later updated at runtime and new implementations can be inlined) Static methods are worth having for clarity so I recommended making classes, fields and method static where possible, but it doesn't make much difference for performance. (A static field can save memory if you have enough instances) –  Peter Lawrey May 7 at 16:28
add comment

There's no significant difference in practice, one way or another. It's especially true in your case, since SQL queries will take much more time than java method call.

As for memory usage, by making method static you're gonna gain 0 bytes of memory, that's absolutely certain.

share|improve this answer
+1 - Indeed, the SQL queries themselves are probably taking 10,000 times or more longer than the method calls you are thinking about "optimizing". –  Stephen C Dec 25 '10 at 13:44
For a static method call, the VM does not have to store this on the stack, so you may save some memory that way but the JIT compiler is probably perfectly capable of optimizing this out for any methods that don't use this. –  Mike Samuel Dec 25 '10 at 14:12
add comment

Your Answer


