Ankur
Ankur

Reputation: 51108

Can I use static methods to improve performance for commonly used methods

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.

Upvotes: 2

Views: 1337

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533870

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.

Upvotes: 7

Nikita Rybak
Nikita Rybak

Reputation: 68036

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.

Upvotes: 8

Related Questions